library(ggplot2)
library(plyr)
library(dplyr)
library(car)
library(fitdistrplus)
library(tidyr)
library(tidyverse)
library(ggtext)
library(lme4)
library(lmerTest)
library(emmeans)
library(glmmTMB)
library(ggbreak)
library(nlme)
library(cxr)
library(MASS)
library(mvtnorm)
library(DescTools)
library(phia)
library(performance)
library(DHARMa)
library(effects)
library(cowplot)
theme_ines<-theme(axis.text = element_text(size=14), axis.title = element_text(size=14, face="bold"), legend.text = element_text(size=12), strip.text = element_text(size=14), plot.title = element_text(size=14, face="bold"), panel.grid=element_line(colour="white"), panel.background = element_rect(fill="white") , axis.line = element_line(size = 0.5, linetype = "solid",
colour = "black"), strip.background = element_rect(fill="white"))
save_plot<-function(dir, width=15, height=10, ...){
ggsave(dir, width = width, height = height, units = c("cm"))
}
Env<-c("Water", "Cadmium")
names(Env)<-c("N", "Cd")
coex<-read.csv("Data_cadmium/Data_for_R/Coexistence Cd_G42_checked.csv", header=TRUE) # Data from the coexistence experiment
ca_raw<-read.csv(file = "Data_cadmium/Data_for_R/CompetitiveAbility Cd_G40_complete.csv", header=TRUE) # cdata from the competitive ability
str(ca_raw)
'data.frame': 3680 obs. of 24 variables:
$ Block : int 1 1 1 1 1 1 1 1 1 1 ...
$ Rep : int 1 1 1 1 1 1 1 1 1 1 ...
$ Box : int 1 1 1 1 1 1 1 1 1 1 ...
$ Leaf : int 3 4 3 4 3 4 3 4 3 4 ...
$ Disk : int 1 2 3 4 5 6 7 8 9 10 ...
$ Env : chr "N" "N" "Cd" "Cd" ...
$ FocalSR : int 4 4 4 4 4 4 4 4 4 4 ...
$ CompSR : int NA NA NA NA NA NA NA NA NA NA ...
$ Dens : int 1 1 1 1 2 2 2 2 4 4 ...
$ Type : chr "INTRA" "INTRA" "INTRA" "INTRA" ...
$ Focalfemale : chr "Te" "Te" "Te" "Te" ...
$ FocalDead : int 0 0 1 0 1 1 0 0 0 0 ...
$ FocalDrowned : int 0 0 0 0 0 0 0 0 0 0 ...
$ FocalMissing : int 0 0 0 0 0 0 0 0 0 0 ...
$ NumbDeadComp : int 0 0 0 0 0 0 0 0 0 0 ...
$ NumberOfEggs : int 11 21 3 9 15 16 17 11 58 24 ...
$ NumberOfEggsBelow : int 0 0 0 0 0 0 0 0 0 0 ...
$ TeMales : int 3 3 0 3 2 5 5 3 8 8 ...
$ TeFemales : int 6 9 2 5 2 10 4 4 9 6 ...
$ TeQuiescentfemales: int 0 0 0 0 0 0 3 0 3 4 ...
$ TuMales : int NA NA NA NA NA NA NA NA NA NA ...
$ TuFemales : int NA NA NA NA NA NA NA NA NA NA ...
$ TuQuiescentfemales: int NA NA NA NA NA NA NA NA NA NA ...
$ Observations : chr NA NA NA NA ...
# Summary of the data to be sure that everything is ok!
summary(as.factor(ca_raw$Foca_rawlSR))
integer(0)
ca_raw$Block2<-as.factor(ca_raw$Block)
ca_raw$Rep2<-as.factor(ca_raw$Rep)
ca_raw$Disk2<-as.factor(ca_raw$Disk)
ca_raw$Leaf2<-as.factor(ca_raw$Leaf)
ca_raw$Env2<-as.factor(ca_raw$Env)
ca_raw$FocalSR2<-as.factor(ca_raw$FocalSR)
ca_raw$CompSR2<-as.factor(ca_raw$CompSR)
ca_raw$Type2<-as.factor(ca_raw$Type)
ca_raw$Focal_Female2<-as.factor(ca_raw$Focalfemale)
regimeTu<-c("Tu \ncontrol", "Tu evolved \n in cadmium")
names(regimeTu)<-c("SR1", "SR2")
regimeTe<-c("Te \n control", "Te evolved \n in cadmium")
names(regimeTe)<-c("SR4", "SR5")
#Creating columns that are needed
ca_raw$Nr_Focal_Females_Tu_Alive_G0<-sapply(c(1:length(ca_raw$Block)), function(x){
if(ca_raw$Focalfemale[x]=="Tu"){
if(ca_raw$Type[x]=="INTRA"){
a<-ca_raw$Dens[x]-ca_raw$FocalDead[x]-ca_raw$FocalDrowned[x]-ca_raw$FocalMissing[x]
}else
a<-1-ca_raw$FocalDead[x]-ca_raw$FocalDrowned[x]-ca_raw$FocalMissing[x]
}else
a<-NA
})
ca_raw$Nr_Focal_Females_Te_Alive_G0<-sapply(c(1:length(ca_raw$Block)), function(x){
if(ca_raw$Focalfemale[x]=="Te"){
if(ca_raw$Type[x]=="INTRA"){
a<-ca_raw$Dens[x]-ca_raw$FocalDead[x]-ca_raw$FocalDrowned[x]-ca_raw$FocalMissing[x]
}else
a<-1-ca_raw$FocalDead[x]-ca_raw$FocalDrowned[x]-ca_raw$FocalMissing[x]
}else
a<-NA
})
ca_raw$Num_Comp_Tu_Alive_G0<-sapply(c(1:length(ca_raw$Block)), function(x){
if(ca_raw$Focalfemale[x]=="Te"){
if(ca_raw$Type[x]=="INTER"){
a<-ca_raw$Dens[x]-ca_raw$NumbDeadComp[x]-1
}else
a<-NA
}else
a<-NA
})
ca_raw$Num_Comp_Te_Alive_G0<-sapply(c(1:length(ca_raw$Block)), function(x){
if(ca_raw$Focalfemale[x]=="Tu"){
if(ca_raw$Type[x]=="INTER"){
a<-ca_raw$Dens[x]-ca_raw$NumbDeadComp[x]-1
}else
a<-NA
}else
a<-NA
})
ca_raw$Nr_Focal_Females_G0<-sapply(c(1:length(ca_raw$Block)), function(x){
if(ca_raw$Type[x]=="INTRA"){
a<-ca_raw$Dens[x]
}else
a<-1
})
ca_raw$Nr_Comp_Females_G0<-sapply(c(1:length(ca_raw$Block)), function(x){
a<-ca_raw$Dens[x]-1
a
})
# Removing rows where there were less than 0 females
ca_raw<-ca_raw[-c(which(ca_raw$Num_Comp_Te_Alive_G0<0),which(ca_raw$Num_Comp_Tu_Alive_G0<0), which(ca_raw$Nr_Focal_Females_Te_Alive_G0<0),which(ca_raw$Nr_Focal_Females_Tu_Alive_G0<0) ),]
# Removing virgin females
ca_raw_virgin<-ca_raw[c(which(ca_raw$TeFemales==0 &ca_raw$TeMales>0 & ca_raw$Focalfemale=="Te" ),which(ca_raw$TuFemales==0 &ca_raw$TuMales>0 & ca_raw$Focalfemale=="Tu" )),]
ca<-ca_raw[-c(which(ca_raw$TeFemales==0 &ca_raw$TeMales>0 & ca_raw$Focalfemale=="Te" ),which(ca_raw$TuFemales==0 &ca_raw$TuMales>0 & ca_raw$Focalfemale=="Tu" )),]
ca[,c("Nr_Focal_Females_G0", "Dens", "Type")]
ca$GrowthRateOA<-sapply(c(1:length(ca[,1])), function(x){
#print(x)
if(ca$Focal_Female[x]=="Tu"){
a<-ca$TuFemales[x]/ca$Nr_Focal_Females_G0[x]
}else if(ca$Focal_Female[x]=="Te"){
a<-ca$TeFemales[x]/ca$Nr_Focal_Females_G0[x]
}else
a<-NA
a
})
ca$GrowthRateOA_Q<-sapply(c(1:length(ca[,1])), function(x){
#print(x)
if(ca$Focal_Female[x]=="Tu"){
a<-(ca$TuFemales[x]+ca$TuQuiescentfemales[x])/ca$Nr_Focal_Females_G0[x]
}else if(ca$Focal_Female[x]=="Te"){
a<-(ca$TeFemales[x]+ca$TeQuiescentfemales[x])/ca$Nr_Focal_Females_G0[x]
}else
a<-NA
a
})
Here we have two differeny methods, using the cxr package or with the optim. We will also vary if we estimate lambda from the data or from the model and if using cxr with the nested approach is better or not. So the different hypothesis are
A - CXR normal: using cxr with the normal approach B - CXR lambda fixed: using cxr but lambda comes from the data C - CXR nested: lambda comes the data, and we use the same nested approach as the optim –> for that we can put intra as another species (column) D - optim normal: the same approach as used in Fragata 2022 E - optim lambda fixed: using optim, but lambda is fixed
In all the models we will use density -1 for the intra, which basically corresponds to the number of competitors.
cxr accepts a data frame with a first column called fitness with positive values and numeric columns with number of individuals. Each row is one individual. For multiple species the easier is to create a list, each with a data frame that has in the first column number of individuals produced and then the number of neighbours
this case we transformed all 0s into 1 (so that the log is 0) For that we need to add +1 to all data so that the variance is not changed
dir.create("./NewAnalyses/cxr_normal", showWarnings = FALSE)
# modifying data frame to fit the type of setup that is need for CXR
forCXR_N<-subset(ca, Env=="N")[,c("Rep", "FocalSR", "CompSR", "Dens", "TeFemales", "TuFemales")]
forCXR_N$Focal<-mapvalues(forCXR_N$FocalSR, c(1,2,4,5), c("SR1", "SR2","SR4","SR5"))
forCXR_N$CompSR2<-mapvalues(forCXR_N$CompSR, c(1,2,4,5), c("SR1", "SR2","SR4","SR5"))
forCXR_N$Comp<-sapply(c(1:length(forCXR_N[,1])), function(x){
if(is.na(forCXR_N$CompSR2[x])){
a<- forCXR_N$Focal[x]
}else{
a<-forCXR_N$CompSR2[x]
}
a
})
aux<-data.frame(SR1=rep(0, length(forCXR_N[,1])), SR2=rep(0, length(forCXR_N[,1])), SR4=rep(0, length(forCXR_N[,1])), SR5=rep(0, length(forCXR_N[,1])))
for(i in 1:length(forCXR_N[,1])){
#coluna onde por focais
colunaF<-which(colnames(aux)==forCXR_N$Focal[i])
#coluna onde por competidors
colunaC<-which(colnames(aux)==forCXR_N$Comp[i])
#if its the same regime
if(forCXR_N$Focal[i]==forCXR_N$Comp[i] & forCXR_N$Dens[i]==1){
aux[i,colunaF]<-forCXR_N$Dens[i]-1
}else if(forCXR_N$Focal[i]==forCXR_N$Comp[i]){
aux[i,colunaF]<-forCXR_N$Dens[i]-1
}else{ #if it is heterospecific then its -1 for the competitors (because of the focal) and its one for the focal
aux[i,colunaC]<-forCXR_N$Dens[i]-1
aux[i, colunaF]<-1
}
}
forCXR_N<-cbind(forCXR_N, aux)
forCXR_N$fitness<-sapply(c(1:length(forCXR_N[,1])), function(x){
colF<-which(colnames(forCXR_N)==forCXR_N$Focal[x])
if(forCXR_N$Focal[x]=="SR1"){
a<-forCXR_N$TuFemales[x]/forCXR_N$SR1[x]
} else if(forCXR_N$Focal[x]=="SR2"){
a<-forCXR_N$TuFemales[x]/forCXR_N$SR2[x]
} else if(forCXR_N$Focal[x]=="SR4"){
a<-forCXR_N$TeFemales[x]/forCXR_N$SR4[x]
} else if(forCXR_N$Focal[x]=="SR5"){
a<-forCXR_N$TeFemales[x]/forCXR_N$SR5[x]
}
a
})
#removing rows for which there is no data for fitness
forCXR_N<-forCXR_N[-which(is.na(forCXR_N$fitness)),]
# adding +1 to all data
#forCXR_N$fitness<-forCXR_N$fitness+1
forCXR_N[which(forCXR_N$fitness=="-Inf" | forCXR_N$fitness=="Inf"),"fitness"]<-0
# all data gets +1 because of the 0 problem
forCXR_N$fitness<-forCXR_N$fitness+1
# vector that tells which are the selection regimes, the columns have to have the same name
my.reg <- c("SR1", "SR2","SR4","SR5")
# Do list per replicate and environment
R1<-list(SR1= subset(forCXR_N, Rep==1 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(forCXR_N, Rep==1 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(forCXR_N, Rep==1 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(forCXR_N, Rep==1 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
R2<-list(SR1= subset(forCXR_N, Rep==2 & Focal=="SR1")[,c("fitness", "SR1", "SR4", "SR5")], SR4= subset(forCXR_N, Rep==2 & Focal=="SR4")[,c("fitness", "SR1", "SR4", "SR5")], SR5= subset(forCXR_N, Rep==2 & Focal=="SR5")[,c("fitness", "SR1", "SR4", "SR5")])
R3<-list(SR1= subset(forCXR_N, Rep==3 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(forCXR_N, Rep==3 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(forCXR_N, Rep==3 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(forCXR_N, Rep==3 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
R4<-list(SR1= subset(forCXR_N, Rep==4 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(forCXR_N, Rep==4 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(forCXR_N, Rep==4 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(forCXR_N, Rep==4 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
R5<-list(SR1= subset(forCXR_N, Rep==5 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(forCXR_N, Rep==5 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(forCXR_N, Rep==5 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(forCXR_N, Rep==5 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
obs.R1_w0<-cxr_pm_multifit(data = R1,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(lambda = 1,
alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = NULL,
# no standard errors
bootstrap_samples = 200)
str(obs.R1_w0)
List of 16
$ model_name : chr "RK_pm_alpha_pairwise_lambdacov_none_alphacov_none"
$ data :List of 4
..$ SR1:'data.frame': 83 obs. of 5 variables:
.. ..$ fitness: num [1:83] 1 5 11 1 6 ...
.. ..$ SR1 : num [1:83] 0 1 1 3 3 9 9 0 1 3 ...
.. ..$ SR2 : num [1:83] 0 0 0 0 0 0 0 0 0 0 ...
.. ..$ SR4 : num [1:83] 0 0 0 0 0 0 0 0 0 0 ...
.. ..$ SR5 : num [1:83] 0 0 0 0 0 0 0 0 0 0 ...
..$ SR2:'data.frame': 83 obs. of 5 variables:
.. ..$ fitness: num [1:83] 1 1 3 2 4.33 ...
.. ..$ SR1 : num [1:83] 0 0 0 0 0 0 0 0 0 0 ...
.. ..$ SR2 : num [1:83] 0 0 1 3 3 9 9 0 0 1 ...
.. ..$ SR4 : num [1:83] 0 0 0 0 0 0 0 0 0 0 ...
.. ..$ SR5 : num [1:83] 0 0 0 0 0 0 0 0 0 0 ...
..$ SR4:'data.frame': 92 obs. of 5 variables:
.. ..$ fitness: num [1:92] 1 1 3 11 4 ...
.. ..$ SR1 : num [1:92] 0 0 0 0 0 0 0 0 0 0 ...
.. ..$ SR2 : num [1:92] 0 0 0 0 0 0 0 0 0 0 ...
.. ..$ SR4 : num [1:92] 0 0 1 1 3 3 9 9 0 0 ...
.. ..$ SR5 : num [1:92] 0 0 0 0 0 0 0 0 0 0 ...
..$ SR5:'data.frame': 90 obs. of 5 variables:
.. ..$ fitness: num [1:90] 1 6 12 10.7 13.7 ...
.. ..$ SR1 : num [1:90] 0 0 0 0 0 0 0 0 0 0 ...
.. ..$ SR2 : num [1:90] 0 0 0 0 0 0 0 0 0 0 ...
.. ..$ SR4 : num [1:90] 0 0 0 0 0 0 0 0 0 0 ...
.. ..$ SR5 : num [1:90] 0 1 1 3 3 9 9 0 1 1 ...
$ taxa : chr [1:4] "SR1" "SR2" "SR4" "SR5"
$ optimization_method : chr "Nelder-Mead"
$ initial_values :List of 3
..$ lambda : num 1
..$ alpha_intra: num 0.1
..$ alpha_inter: num 0.1
$ fixed_terms : NULL
$ lambda : Named num [1:4] 2.79 2.07 4.84 5.07
..- attr(*, "names")= chr [1:4] "SR1" "SR2" "SR4" "SR5"
$ alpha_matrix : num [1:4, 1:4] 0.0623 NA 0.0851 0.0432 NA ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:4] "SR1" "SR2" "SR4" "SR5"
.. ..$ : chr [1:4] "SR1" "SR2" "SR4" "SR5"
$ lambda_cov : NULL
$ alpha_cov : NULL
$ lambda_standard_error : Named num [1:4] 0.386 0.275 0.998 1.002
..- attr(*, "names")= chr [1:4] "SR1" "SR2" "SR4" "SR5"
$ alpha_standard_error : NULL
$ lambda_cov_standard_error : NULL
$ alpha_cov_standard_error : NULL
$ log_likelihood : Named num [1:4] 88.7 81 133 124.4
..- attr(*, "names")= chr [1:4] "SR1" "SR2" "SR4" "SR5"
$ alpha_matrix_standard_error: num [1:4, 1:4] 0.0235 NA 0.0458 0.0447 NA ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:4] "SR1" "SR2" "SR4" "SR5"
.. ..$ : chr [1:4] "SR1_se" "SR2_se" "SR4_se" "SR5_se"
- attr(*, "class")= chr "cxr_pm_multifit"
obs.R1_w0$lambda_standard_error
SR1 SR2 SR4 SR5
0.3860811 0.2753301 0.9980740 1.0016123
obs.R1_w0$alpha_matrix_standard_error
SR1_se SR2_se SR4_se SR5_se
SR1 0.02345024 NA 0.03819883 0.02690249
SR2 NA 0.02238806 0.02908016 0.02955404
SR4 0.04577716 0.03797263 0.04069716 NA
SR5 0.04469513 0.04436930 NA 0.03718012
obs.R3_w0<-cxr_pm_multifit(data = R3,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(lambda = 1,
alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = NULL,
# no standard errors
bootstrap_samples = 200)
obs.R4_w0<-cxr_pm_multifit(data = R4,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(lambda = 1,
alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = NULL,
# no standard errors
bootstrap_samples = 200)
obs.R5_w0<-cxr_pm_multifit(data = R5,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(lambda = 1,
alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = NULL,
# no standard errors
bootstrap_samples = 200)
summary(obs.R1_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4
SR1 0.06226589 NA 0.02111418
SR2 NA -0.006361465 0.01646337
SR4 0.08509094 0.054815348 0.02134567
SR5 0.04318067 0.100217430 NA
SR5
SR1 0.04056630
SR2 0.01664879
SR4 NA
SR5 0.02589732
summary(obs.R3_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4
SR1 0.08313077 NA 0.07025388
SR2 NA 0.029102575 0.02698001
SR4 0.06631461 0.034909359 0.01113335
SR5 0.02349603 0.004732142 NA
SR5
SR1 0.055951893
SR2 0.004947367
SR4 NA
SR5 0.055838552
summary(obs.R4_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4
SR1 0.03224502 NA 0.04894165
SR2 NA 0.074574879 0.05857140
SR4 0.05809101 0.008017659 -0.01334212
SR5 0.08168851 0.071000640 NA
SR5
SR1 0.06841501
SR2 0.03011274
SR4 NA
SR5 0.05467569
summary(obs.R5_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4
SR1 -0.001899959 NA 0.03713201
SR2 NA 0.04267937 0.07168209
SR4 0.071993005 0.09790030 0.03595535
SR5 0.124698580 0.09067722 NA
SR5
SR1 0.009281002
SR2 -0.031603393
SR4 NA
SR5 0.045018753
# For replicate 2 we need to do it differently
obs.R2_w0_sr1<-cxr_pm_fit(data = R2[[1]],
focal_column = my.reg[1],
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(lambda = 1,
alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = NULL,
# no standard errors
bootstrap_samples = 200)
obs.R2_w0_sr4<-cxr_pm_fit(data = R2[[2]][which(R2[[2]][,"SR1"]==0),c("fitness", "SR4")],
focal_column =NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(lambda = 1,
alpha_inter = 0.1),
fixed_terms = NULL,
# no standard errors
bootstrap_samples = 200)
obs.R2_w0_sr4_inter<-cxr_pm_fit(data = R2[[2]][which(R2[[2]][,"SR1"]!=0),c("fitness", "SR4")],
focal_column =NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = list(lambda=obs.R2_w0_sr4$lambda),
# no standard errors
bootstrap_samples = 200)
obs.R2_w0_sr5<-cxr_pm_fit(data = R2[[3]][which(R2[[3]][,"SR1"]==0),c("fitness", "SR5")],
focal_column =NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(lambda = 1,
alpha_inter = 0.1),
fixed_terms = NULL,
# no standard errors
bootstrap_samples = 200)
obs.R2_w0_sr5_inter<-cxr_pm_fit(data = R2[[3]][which(R2[[3]][,"SR1"]!=0),c("fitness", "SR5")],
focal_column =NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = list(lambda=obs.R2_w0_sr5$lambda),
# no standard errors
bootstrap_samples = 200)
#ab<-abundance_projection(obs.R1_w0, timesteps = 1, initial_abundances = c(3,3,3,3))
rows in the alpha element of the returning list correspond to species i and columns to species j for each αij coefficient.
cxr_param_w0<-expand.grid(Tu_Regime=c("SR1", "SR2"), Te_Regime=c("SR4", "SR5"), Replicate=c(1,2,3,4,5), Environment=c("N"))
cxr_param_w0$Tu_lambda<-0
cxr_param_w0$Te_lambda<-0
cxr_param_w0$Tu_intra<-0
cxr_param_w0$Te_intra<-0
cxr_param_w0$Tu_inter<-0
cxr_param_w0$Te_inter<-0
#removing SR2 for replicate 2
cxr_param_w0<-cxr_param_w0[-which(cxr_param_w0$Replicate==2 & cxr_param_w0$Tu_Regime=="SR2"),]
cxr_param_w0[which(cxr_param_w0$Replicate==1),"Tu_lambda"]<-obs.R1_w0$lambda[1:2]
cxr_param_w0[which(cxr_param_w0$Replicate==1),"Te_lambda"]<-obs.R1_w0$lambda[c(3,3,4,4)]
cxr_param_w0[which(cxr_param_w0$Replicate==2),"Tu_lambda"]<-obs.R2_w0_sr1$lambda
cxr_param_w0[which(cxr_param_w0$Replicate==2),"Te_lambda"]<-c(obs.R2_w0_sr4$lambda,obs.R2_w0_sr5$lambda)
cxr_param_w0[which(cxr_param_w0$Replicate==3),"Tu_lambda"]<-obs.R3_w0$lambda[1:2]
cxr_param_w0[which(cxr_param_w0$Replicate==3),"Te_lambda"]<-obs.R3_w0$lambda[c(3,3,4,4)]
cxr_param_w0[which(cxr_param_w0$Replicate==4),"Tu_lambda"]<-obs.R4_w0$lambda[1:2]
cxr_param_w0[which(cxr_param_w0$Replicate==4),"Te_lambda"]<-obs.R4_w0$lambda[c(3,3,4,4)]
cxr_param_w0[which(cxr_param_w0$Replicate==5),"Tu_lambda"]<-obs.R5_w0$lambda[1:2]
cxr_param_w0[which(cxr_param_w0$Replicate==5),"Te_lambda"]<-obs.R5_w0$lambda[c(3,3,4,4)]
cxr_param_w0[which(cxr_param_w0$Replicate==1),"Tu_intra"]<-rep(c(obs.R1_w0$alpha_matrix[1,1], obs.R1_w0$alpha_matrix[2,2]), 2)
cxr_param_w0[which(cxr_param_w0$Replicate==1),"Te_intra"]<-rep(c(obs.R1_w0$alpha_matrix[3,3], obs.R1_w0$alpha_matrix[4,4]), each=2)
cxr_param_w0[which(cxr_param_w0$Replicate==2),"Tu_intra"]<-obs.R2_w0_sr1$alpha_intra
cxr_param_w0[which(cxr_param_w0$Replicate==2),"Te_intra"]<-c(obs.R2_w0_sr4$alpha_inter, obs.R2_w0_sr5$alpha_inter)
cxr_param_w0[which(cxr_param_w0$Replicate==3),"Tu_intra"]<-rep(c(obs.R3_w0$alpha_matrix[1,1], obs.R3_w0$alpha_matrix[2,2]), 2)
cxr_param_w0[which(cxr_param_w0$Replicate==3),"Te_intra"]<-rep(c(obs.R3_w0$alpha_matrix[3,3], obs.R3_w0$alpha_matrix[4,4]), each=2)
cxr_param_w0[which(cxr_param_w0$Replicate==4),"Tu_intra"]<-rep(c(obs.R4_w0$alpha_matrix[1,1], obs.R4_w0$alpha_matrix[2,2]), 2)
cxr_param_w0[which(cxr_param_w0$Replicate==4),"Te_intra"]<-rep(c(obs.R4_w0$alpha_matrix[3,3], obs.R4_w0$alpha_matrix[4,4]), each=2)
cxr_param_w0[which(cxr_param_w0$Replicate==5),"Tu_intra"]<-rep(c(obs.R5_w0$alpha_matrix[1,1], obs.R5_w0$alpha_matrix[2,2]), 2)
cxr_param_w0[which(cxr_param_w0$Replicate==5),"Te_intra"]<-rep(c(obs.R5_w0$alpha_matrix[3,3], obs.R5_w0$alpha_matrix[4,4]), each=2)
cxr_param_w0[which(cxr_param_w0$Replicate==1),"Tu_inter"]<-c(obs.R1_w0$alpha_matrix[1,3], obs.R1_w0$alpha_matrix[2,3],obs.R1_w0$alpha_matrix[1,4], obs.R1_w0$alpha_matrix[2,4])
cxr_param_w0[which(cxr_param_w0$Replicate==1),"Te_inter"]<-c(obs.R1_w0$alpha_matrix[3,1], obs.R1_w0$alpha_matrix[3,2],obs.R1_w0$alpha_matrix[4,1], obs.R1_w0$alpha_matrix[4,2])
cxr_param_w0[which(cxr_param_w0$Replicate==2),"Tu_inter"]<-obs.R2_w0_sr1$alpha_inter
cxr_param_w0[which(cxr_param_w0$Replicate==2),"Te_inter"]<-c(obs.R2_w0_sr4_inter$alpha_inter, obs.R2_w0_sr5_inter$alpha_inter)
cxr_param_w0[which(cxr_param_w0$Replicate==3),"Tu_inter"]<-c(obs.R3_w0$alpha_matrix[1,3], obs.R3_w0$alpha_matrix[2,3],obs.R3_w0$alpha_matrix[1,4], obs.R3_w0$alpha_matrix[2,4])
cxr_param_w0[which(cxr_param_w0$Replicate==3),"Te_inter"]<-c(obs.R3_w0$alpha_matrix[3,1], obs.R3_w0$alpha_matrix[3,2],obs.R3_w0$alpha_matrix[4,1], obs.R3_w0$alpha_matrix[4,2])
cxr_param_w0[which(cxr_param_w0$Replicate==4),"Tu_inter"]<-c(obs.R4_w0$alpha_matrix[1,3], obs.R4_w0$alpha_matrix[2,3],obs.R4_w0$alpha_matrix[1,4], obs.R4_w0$alpha_matrix[2,4])
cxr_param_w0[which(cxr_param_w0$Replicate==4),"Te_inter"]<-c(obs.R4_w0$alpha_matrix[3,1], obs.R4_w0$alpha_matrix[3,2],obs.R4_w0$alpha_matrix[4,1], obs.R4_w0$alpha_matrix[4,2])
cxr_param_w0[which(cxr_param_w0$Replicate==5),"Tu_inter"]<-c(obs.R5_w0$alpha_matrix[1,3], obs.R5_w0$alpha_matrix[2,3],obs.R5_w0$alpha_matrix[1,4], obs.R5_w0$alpha_matrix[2,4])
cxr_param_w0[which(cxr_param_w0$Replicate==5),"Te_inter"]<-c(obs.R5_w0$alpha_matrix[3,1], obs.R5_w0$alpha_matrix[3,2],obs.R5_w0$alpha_matrix[4,1], obs.R5_w0$alpha_matrix[4,2])
### Lower
cxr_param_w0_lower<-expand.grid(Tu_Regime=c("SR1", "SR2"), Te_Regime=c("SR4", "SR5"), Replicate=c(1,2,3,4,5), Environment=c("N"))
cxr_param_w0_lower$Tu_lambda<-0
cxr_param_w0_lower$Te_lambda<-0
cxr_param_w0_lower$Tu_intra<-0
cxr_param_w0_lower$Te_intra<-0
cxr_param_w0_lower$Tu_inter<-0
cxr_param_w0_lower$Te_inter<-0
#removing SR2 for replicate 2
cxr_param_w0_lower<-cxr_param_w0_lower[-which(cxr_param_w0_lower$Replicate==2 & cxr_param_w0_lower$Tu_Regime=="SR2"),]
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==1),"Tu_lambda"]<-rep(c(obs.R1_w0$lambda[1]-obs.R1_w0$lambda_standard_error[1], obs.R1_w0$lambda[2]-obs.R1_w0$lambda_standard_error[2]), 2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==1),"Te_lambda"]<-rep(c(obs.R1_w0$lambda[3]-obs.R1_w0$lambda_standard_error[3], obs.R1_w0$lambda[4]-obs.R1_w0$lambda_standard_error[4]), each=2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==2),"Tu_lambda"]<-obs.R2_w0_sr1$lambda-obs.R2_w0_sr1$lambda_standard_error
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==2),"Te_lambda"]<-c(obs.R2_w0_sr4$lambda-obs.R2_w0_sr4$lambda_standard_error,obs.R2_w0_sr5$lambda-obs.R2_w0_sr5$lambda_standard_error)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==3),"Tu_lambda"]<-rep(c(obs.R3_w0$lambda[1]-obs.R3_w0$lambda_standard_error[1], obs.R3_w0$lambda[2]-obs.R3_w0$lambda_standard_error[2]), 2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==3),"Te_lambda"]<-rep(c(obs.R3_w0$lambda[3]-obs.R3_w0$lambda_standard_error[3], obs.R3_w0$lambda[4]-obs.R3_w0$lambda_standard_error[4]), each=2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==4),"Tu_lambda"]<-rep(c(obs.R4_w0$lambda[1]-obs.R4_w0$lambda_standard_error[1], obs.R4_w0$lambda[2]-obs.R4_w0$lambda_standard_error[2]), 2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==4),"Te_lambda"]<-rep(c(obs.R4_w0$lambda[3]-obs.R4_w0$lambda_standard_error[3], obs.R4_w0$lambda[4]-obs.R4_w0$lambda_standard_error[4]), each=2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==5),"Tu_lambda"]<-rep(c(obs.R5_w0$lambda[1]-obs.R5_w0$lambda_standard_error[1], obs.R5_w0$lambda[2]-obs.R5_w0$lambda_standard_error[2]), 2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==5),"Te_lambda"]<-rep(c(obs.R5_w0$lambda[3]-obs.R5_w0$lambda_standard_error[3], obs.R5_w0$lambda[4]-obs.R5_w0$lambda_standard_error[4]), each=2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==1),"Tu_intra"]<-rep(c(obs.R1_w0$alpha_matrix[1,1]-obs.R1_w0$alpha_matrix_standard_error[1,1], obs.R1_w0$alpha_matrix[2,2]-obs.R1_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==1),"Te_intra"]<-rep(c(obs.R1_w0$alpha_matrix[3,3]-obs.R1_w0$alpha_matrix_standard_error[3,3], obs.R1_w0$alpha_matrix[4,4]-obs.R1_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==2),"Tu_intra"]<-obs.R2_w0_sr1$alpha_intra-obs.R2_w0_sr1$alpha_intra_standard_error
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==2),"Te_intra"]<-c(obs.R2_w0_sr4$alpha_inter-obs.R2_w0_sr4$alpha_inter_standard_error, obs.R2_w0_sr5$alpha_inter-obs.R2_w0_sr5$alpha_inter_standard_error)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==3),"Tu_intra"]<-rep(c(obs.R3_w0$alpha_matrix[1,1]-obs.R3_w0$alpha_matrix_standard_error[1,1], obs.R3_w0$alpha_matrix[2,2]-obs.R3_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==3),"Te_intra"]<-rep(c(obs.R3_w0$alpha_matrix[3,3]-obs.R3_w0$alpha_matrix_standard_error[3,3], obs.R3_w0$alpha_matrix[4,4]-obs.R3_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==4),"Tu_intra"]<-rep(c(obs.R4_w0$alpha_matrix[1,1]-obs.R4_w0$alpha_matrix_standard_error[1,1], obs.R4_w0$alpha_matrix[2,2]-obs.R4_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==4),"Te_intra"]<-rep(c(obs.R4_w0$alpha_matrix[3,3]-obs.R4_w0$alpha_matrix_standard_error[3,3], obs.R4_w0$alpha_matrix[4,4]-obs.R4_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==5),"Tu_intra"]<-rep(c(obs.R5_w0$alpha_matrix[1,1]-obs.R5_w0$alpha_matrix_standard_error[1,1], obs.R5_w0$alpha_matrix[2,2]-obs.R5_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==5),"Te_intra"]<-rep(c(obs.R5_w0$alpha_matrix[3,3]-obs.R5_w0$alpha_matrix_standard_error[3,3], obs.R5_w0$alpha_matrix[4,4]-obs.R5_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==1),"Tu_inter"]<-c(obs.R1_w0$alpha_matrix[1,3]-obs.R1_w0$alpha_matrix_standard_error[1,3], obs.R1_w0$alpha_matrix[2,3]-obs.R1_w0$alpha_matrix_standard_error[2,3],obs.R1_w0$alpha_matrix[1,4]-obs.R1_w0$alpha_matrix_standard_error[1,4], obs.R1_w0$alpha_matrix[2,4]-obs.R1_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==1),"Te_inter"]<-c(obs.R1_w0$alpha_matrix[3,1]-obs.R1_w0$alpha_matrix_standard_error[3,1], obs.R1_w0$alpha_matrix[3,2]-obs.R1_w0$alpha_matrix_standard_error[3,2],obs.R1_w0$alpha_matrix[4,1]-obs.R1_w0$alpha_matrix_standard_error[4,1], obs.R1_w0$alpha_matrix[4,2]-obs.R1_w0$alpha_matrix_standard_error[4,2])
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==2),"Tu_inter"]<-obs.R2_w0_sr1$alpha_inter-obs.R2_w0_sr1$alpha_inter_standard_error
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==2),"Te_inter"]<-c(obs.R2_w0_sr4_inter$alpha_inter-obs.R2_w0_sr4_inter$alpha_inter_standard_error, obs.R2_w0_sr5_inter$alpha_inter-obs.R2_w0_sr5_inter$alpha_inter_standard_error)
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==3),"Tu_inter"]<-c(obs.R3_w0$alpha_matrix[1,3]-obs.R3_w0$alpha_matrix_standard_error[1,3], obs.R3_w0$alpha_matrix[2,3]-obs.R3_w0$alpha_matrix_standard_error[2,3],obs.R3_w0$alpha_matrix[1,4]-obs.R3_w0$alpha_matrix_standard_error[1,4], obs.R3_w0$alpha_matrix[2,4]-obs.R3_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==3),"Te_inter"]<-c(obs.R3_w0$alpha_matrix[3,1]-obs.R3_w0$alpha_matrix_standard_error[3,1], obs.R3_w0$alpha_matrix[3,2]-obs.R3_w0$alpha_matrix_standard_error[3,2],obs.R3_w0$alpha_matrix[4,1]-obs.R3_w0$alpha_matrix_standard_error[4,1], obs.R3_w0$alpha_matrix[4,2]-obs.R3_w0$alpha_matrix_standard_error[4,2])
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==4),"Tu_inter"]<-c(obs.R4_w0$alpha_matrix[1,3]-obs.R4_w0$alpha_matrix_standard_error[1,3], obs.R4_w0$alpha_matrix[2,3]-obs.R4_w0$alpha_matrix_standard_error[2,3],obs.R4_w0$alpha_matrix[1,4]-obs.R4_w0$alpha_matrix_standard_error[1,4], obs.R4_w0$alpha_matrix[2,4]-obs.R4_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==4),"Te_inter"]<-c(obs.R4_w0$alpha_matrix[3,1]-obs.R4_w0$alpha_matrix_standard_error[3,1], obs.R4_w0$alpha_matrix[3,2]-obs.R4_w0$alpha_matrix_standard_error[3,2],obs.R4_w0$alpha_matrix[4,1]-obs.R4_w0$alpha_matrix_standard_error[4,1], obs.R4_w0$alpha_matrix[4,2]-obs.R4_w0$alpha_matrix_standard_error[4,2])
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==5),"Tu_inter"]<-c(obs.R5_w0$alpha_matrix[1,3]-obs.R5_w0$alpha_matrix_standard_error[1,3], obs.R5_w0$alpha_matrix[2,3]-obs.R5_w0$alpha_matrix_standard_error[2,3],obs.R5_w0$alpha_matrix[1,4]-obs.R5_w0$alpha_matrix_standard_error[1,4], obs.R5_w0$alpha_matrix[2,4]-obs.R5_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0_lower[which(cxr_param_w0_lower$Replicate==5),"Te_inter"]<-c(obs.R5_w0$alpha_matrix[3,1]-obs.R5_w0$alpha_matrix_standard_error[3,1], obs.R5_w0$alpha_matrix[3,2]-obs.R5_w0$alpha_matrix_standard_error[3,2],obs.R5_w0$alpha_matrix[4,1]-obs.R5_w0$alpha_matrix_standard_error[4,1], obs.R5_w0$alpha_matrix[4,2]-obs.R5_w0$alpha_matrix_standard_error[4,2])
### upper
cxr_param_w0_upper<-expand.grid(Tu_Regime=c("SR1", "SR2"), Te_Regime=c("SR4", "SR5"), Replicate=c(1,2,3,4,5), Environment=c("N"))
cxr_param_w0_upper$Tu_lambda<-0
cxr_param_w0_upper$Te_lambda<-0
cxr_param_w0_upper$Tu_intra<-0
cxr_param_w0_upper$Te_intra<-0
cxr_param_w0_upper$Tu_inter<-0
cxr_param_w0_upper$Te_inter<-0
#removing SR2 for replicate 2
cxr_param_w0_upper<-cxr_param_w0_upper[-which(cxr_param_w0_upper$Replicate==2 & cxr_param_w0_upper$Tu_Regime=="SR2"),]
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==1),"Tu_lambda"]<-rep(c(obs.R1_w0$lambda[1]+obs.R1_w0$lambda_standard_error[1], obs.R1_w0$lambda[2]+obs.R1_w0$lambda_standard_error[2]), 2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==1),"Te_lambda"]<-rep(c(obs.R1_w0$lambda[3]+obs.R1_w0$lambda_standard_error[3], obs.R1_w0$lambda[4]+obs.R1_w0$lambda_standard_error[4]), each=2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==2),"Tu_lambda"]<-obs.R2_w0_sr1$lambda+ obs.R2_w0_sr1$lambda_standard_error
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==2),"Te_lambda"]<-c(obs.R2_w0_sr4$lambda+obs.R2_w0_sr4$lambda_standard_error, obs.R2_w0_sr5$lambda+obs.R2_w0_sr5$lambda_standard_error)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==3),"Tu_lambda"]<-rep(c(obs.R3_w0$lambda[1]+obs.R3_w0$lambda_standard_error[1], obs.R3_w0$lambda[2]+obs.R3_w0$lambda_standard_error[2]), 2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==3),"Te_lambda"]<-rep(c(obs.R3_w0$lambda[3]+obs.R3_w0$lambda_standard_error[3], obs.R3_w0$lambda[4]+obs.R3_w0$lambda_standard_error[4]), each=2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==4),"Tu_lambda"]<-rep(c(obs.R4_w0$lambda[1]+obs.R4_w0$lambda_standard_error[1], obs.R4_w0$lambda[2]+obs.R4_w0$lambda_standard_error[2]), 2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==4),"Te_lambda"]<-rep(c(obs.R4_w0$lambda[3]+obs.R4_w0$lambda_standard_error[3], obs.R4_w0$lambda[4]+obs.R4_w0$lambda_standard_error[4]), each=2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==5),"Tu_lambda"]<-rep(c(obs.R5_w0$lambda[1]+obs.R5_w0$lambda_standard_error[1], obs.R5_w0$lambda[2]+obs.R5_w0$lambda_standard_error[2]), 2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==5),"Te_lambda"]<-rep(c(obs.R5_w0$lambda[3]+obs.R5_w0$lambda_standard_error[3], obs.R5_w0$lambda[4]+obs.R5_w0$lambda_standard_error[4]), each=2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==1),"Tu_intra"]<-rep(c(obs.R1_w0$alpha_matrix[1,1]+obs.R1_w0$alpha_matrix_standard_error[1,1], obs.R1_w0$alpha_matrix[2,2]+obs.R1_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==1),"Te_intra"]<-rep(c(obs.R1_w0$alpha_matrix[3,3]+obs.R1_w0$alpha_matrix_standard_error[3,3], obs.R1_w0$alpha_matrix[4,4]+obs.R1_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==2),"Tu_intra"]<-obs.R2_w0_sr1$alpha_intra+obs.R2_w0_sr1$alpha_intra_standard_error
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==2),"Te_intra"]<-c(obs.R2_w0_sr4$alpha_inter+obs.R2_w0_sr4$alpha_inter_standard_error, obs.R2_w0_sr5$alpha_inter+obs.R2_w0_sr5$alpha_inter_standard_error)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==3),"Tu_intra"]<-rep(c(obs.R3_w0$alpha_matrix[1,1]+obs.R3_w0$alpha_matrix_standard_error[1,1], obs.R3_w0$alpha_matrix[2,2]+obs.R3_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==3),"Te_intra"]<-rep(c(obs.R3_w0$alpha_matrix[3,3]+obs.R3_w0$alpha_matrix_standard_error[3,3], obs.R3_w0$alpha_matrix[4,4]+obs.R3_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==4),"Tu_intra"]<-rep(c(obs.R4_w0$alpha_matrix[1,1]+obs.R4_w0$alpha_matrix_standard_error[1,1], obs.R4_w0$alpha_matrix[2,2]+obs.R4_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==4),"Te_intra"]<-rep(c(obs.R4_w0$alpha_matrix[3,3]+obs.R4_w0$alpha_matrix_standard_error[3,3], obs.R4_w0$alpha_matrix[4,4]+obs.R4_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==5),"Tu_intra"]<-rep(c(obs.R5_w0$alpha_matrix[1,1]+obs.R5_w0$alpha_matrix_standard_error[1,1], obs.R5_w0$alpha_matrix[2,2]+obs.R5_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==5),"Te_intra"]<-rep(c(obs.R5_w0$alpha_matrix[3,3]+obs.R5_w0$alpha_matrix_standard_error[3,3], obs.R5_w0$alpha_matrix[4,4]+obs.R5_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==1),"Tu_inter"]<-c(obs.R1_w0$alpha_matrix[1,3]+obs.R1_w0$alpha_matrix_standard_error[1,3], obs.R1_w0$alpha_matrix[2,3]+obs.R1_w0$alpha_matrix_standard_error[2,3],obs.R1_w0$alpha_matrix[1,4]+obs.R1_w0$alpha_matrix_standard_error[1,4], obs.R1_w0$alpha_matrix[2,4]+obs.R1_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==1),"Te_inter"]<-c(obs.R1_w0$alpha_matrix[3,1]+obs.R1_w0$alpha_matrix_standard_error[3,1], obs.R1_w0$alpha_matrix[3,2]+obs.R1_w0$alpha_matrix_standard_error[3,2],obs.R1_w0$alpha_matrix[4,1]+obs.R1_w0$alpha_matrix_standard_error[4,1], obs.R1_w0$alpha_matrix[4,2]+obs.R1_w0$alpha_matrix_standard_error[4,2])
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==2),"Tu_inter"]<-c(obs.R2_w0_sr1$alpha_inter+obs.R2_w0_sr1$alpha_inter_standard_error)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==2),"Te_inter"]<-c(obs.R2_w0_sr4_inter$alpha_inter+obs.R2_w0_sr4_inter$alpha_inter_standard_error, obs.R2_w0_sr5_inter$alpha_inter+obs.R2_w0_sr5_inter$alpha_inter_standard_error)
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==3),"Tu_inter"]<-c(obs.R3_w0$alpha_matrix[1,3]+obs.R3_w0$alpha_matrix_standard_error[1,3], obs.R3_w0$alpha_matrix[2,3]+obs.R3_w0$alpha_matrix_standard_error[2,3],obs.R3_w0$alpha_matrix[1,4]+obs.R3_w0$alpha_matrix_standard_error[1,4], obs.R3_w0$alpha_matrix[2,4]+obs.R3_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==3),"Te_inter"]<-c(obs.R3_w0$alpha_matrix[3,1]+obs.R3_w0$alpha_matrix_standard_error[3,1], obs.R3_w0$alpha_matrix[3,2]+obs.R3_w0$alpha_matrix_standard_error[3,2],obs.R3_w0$alpha_matrix[4,1]+obs.R3_w0$alpha_matrix_standard_error[4,1], obs.R3_w0$alpha_matrix[4,2]+obs.R3_w0$alpha_matrix_standard_error[4,2])
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==4),"Tu_inter"]<-c(obs.R4_w0$alpha_matrix[1,3]+obs.R4_w0$alpha_matrix_standard_error[1,3], obs.R4_w0$alpha_matrix[2,3]+obs.R4_w0$alpha_matrix_standard_error[2,3],obs.R4_w0$alpha_matrix[1,4]+obs.R4_w0$alpha_matrix_standard_error[1,4], obs.R4_w0$alpha_matrix[2,4]+obs.R4_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==4),"Te_inter"]<-c(obs.R4_w0$alpha_matrix[3,1]+obs.R4_w0$alpha_matrix_standard_error[3,1], obs.R4_w0$alpha_matrix[3,2]+obs.R4_w0$alpha_matrix_standard_error[3,2],obs.R4_w0$alpha_matrix[4,1]+obs.R4_w0$alpha_matrix_standard_error[4,1], obs.R4_w0$alpha_matrix[4,2]+obs.R4_w0$alpha_matrix_standard_error[4,2])
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==5),"Tu_inter"]<-c(obs.R5_w0$alpha_matrix[1,3]+obs.R5_w0$alpha_matrix_standard_error[1,3], obs.R5_w0$alpha_matrix[2,3]+obs.R5_w0$alpha_matrix_standard_error[2,3],obs.R5_w0$alpha_matrix[1,4]+obs.R5_w0$alpha_matrix_standard_error[1,4], obs.R5_w0$alpha_matrix[2,4]+obs.R5_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0_upper[which(cxr_param_w0_upper$Replicate==5),"Te_inter"]<-c(obs.R5_w0$alpha_matrix[3,1]+obs.R5_w0$alpha_matrix_standard_error[3,1], obs.R5_w0$alpha_matrix[3,2]+obs.R5_w0$alpha_matrix_standard_error[3,2],obs.R5_w0$alpha_matrix[4,1]+obs.R5_w0$alpha_matrix_standard_error[4,1], obs.R5_w0$alpha_matrix[4,2]+obs.R5_w0$alpha_matrix_standard_error[4,2])
# modifying data frame to fit the type of setup that is need for CXR
forCXR_Cd<-subset(ca, Env=="Cd")[,c("Rep", "FocalSR", "CompSR", "Dens", "TeFemales", "TuFemales")]
forCXR_Cd$Focal<-mapvalues(forCXR_Cd$FocalSR, c(1,2,4,5), c("SR1", "SR2","SR4","SR5"))
forCXR_Cd$CompSR2<-mapvalues(forCXR_Cd$CompSR, c(1,2,4,5), c("SR1", "SR2","SR4","SR5"))
forCXR_Cd$Comp<-sapply(c(1:length(forCXR_Cd[,1])), function(x){
if(is.na(forCXR_Cd$CompSR2[x])){
a<- forCXR_Cd$Focal[x]
}else{
a<-forCXR_Cd$CompSR2[x]
}
a
})
aux<-data.frame(SR1=rep(0, length(forCXR_Cd[,1])), SR2=rep(0, length(forCXR_Cd[,1])), SR4=rep(0, length(forCXR_Cd[,1])), SR5=rep(0, length(forCXR_Cd[,1])))
for(i in 1:length(forCXR_Cd[,1])){
#coluna onde por focais
colunaF<-which(colnames(aux)==forCXR_Cd$Focal[i])
#coluna onde por competidors
colunaC<-which(colnames(aux)==forCXR_Cd$Comp[i])
#if its the same regime
if(forCXR_Cd$Focal[i]==forCXR_Cd$Comp[i] & forCXR_Cd$Dens[i]==1){
aux[i,colunaF]<-forCXR_Cd$Dens[i]-1
}else if(forCXR_Cd$Focal[i]==forCXR_Cd$Comp[i]){
aux[i,colunaF]<-forCXR_Cd$Dens[i]-1
}else{ #if it is heterospecific then its -1 for the competitors (because of the focal) and its one for the focal
aux[i,colunaC]<-forCXR_Cd$Dens[i]-1
aux[i, colunaF]<-1
}
}
forCXR_Cd<-cbind(forCXR_Cd, aux)
forCXR_Cd$fitness<-sapply(c(1:length(forCXR_Cd[,1])), function(x){
colF<-which(colnames(forCXR_Cd)==forCXR_Cd$Focal[x])
if(forCXR_Cd$Focal[x]=="SR1"){
a<-forCXR_Cd$TuFemales[x]/forCXR_Cd$SR1[x]
} else if(forCXR_Cd$Focal[x]=="SR2"){
a<-forCXR_Cd$TuFemales[x]/forCXR_Cd$SR2[x]
} else if(forCXR_Cd$Focal[x]=="SR4"){
a<-forCXR_Cd$TeFemales[x]/forCXR_Cd$SR4[x]
} else if(forCXR_Cd$Focal[x]=="SR5"){
a<-forCXR_Cd$TeFemales[x]/forCXR_Cd$SR5[x]
}
a
})
subset(ca, Env=="Cd" & Rep=="2" & FocalSR==5 &Type=="INTER")[,c("Rep", "FocalSR", "CompSR", "Dens", "TeFemales", "Block")]
#removing rows for which there is no data for fitness
#forCXR_Cd<-forCXR_Cd[-which(is.na(forCXR_Cd$fitness)),]
#forCXR_Cd$fitness<-forCXR_Cd$fitness+1
forCXR_Cd[which(forCXR_Cd$fitness=="-Inf" | forCXR_Cd$fitness=="Inf"),"fitness"]<-0
#0 to 1 to mainrain data
forCXR_Cd<-forCXR_Cd[-which(is.na(forCXR_Cd$fitness)),]
forCXR_Cd$fitness<-forCXR_Cd$fitness+1
# vector that tells which are the selection regimes, the columns have to have the same name
my.reg <- c("SR1", "SR2","SR4","SR5")
# Do list per replicate and environment
R1_Cd<-list(SR1= subset(forCXR_Cd, Rep==1 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(forCXR_Cd, Rep==1 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(forCXR_Cd, Rep==1 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(forCXR_Cd, Rep==1 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
R2_Cd<-list(SR1= subset(forCXR_Cd, Rep==2 & Focal=="SR1")[,c("fitness", "SR1", "SR2","SR4", "SR5")], SR4= subset(forCXR_Cd, Rep==2 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(forCXR_Cd, Rep==2 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
R3_Cd<-list(SR1= subset(forCXR_Cd, Rep==3 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(forCXR_Cd, Rep==3 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(forCXR_Cd, Rep==3 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(forCXR_Cd, Rep==3 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
R4_Cd<-list(SR1= subset(forCXR_Cd, Rep==4 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(forCXR_Cd, Rep==4 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(forCXR_Cd, Rep==4 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(forCXR_Cd, Rep==4 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
R5_Cd<-list(SR1= subset(forCXR_Cd, Rep==5 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(forCXR_Cd, Rep==5 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(forCXR_Cd, Rep==5 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(forCXR_Cd, Rep==5 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
obs.R1_Cd_w0<-cxr_pm_multifit(data = R1_Cd,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(lambda = 1,
alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = NULL,
# no standard errors
bootstrap_samples = 200)
# replicate 2 below
obs.R3_Cd_w0<-cxr_pm_multifit(data = R3_Cd,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(lambda = 1,
alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = NULL,
# no standard errors
bootstrap_samples =10)
obs.R4_Cd_w0<-cxr_pm_multifit(data = R4_Cd,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(lambda = 1,
alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = NULL,
# no standard errors
bootstrap_samples = 200)
obs.R5_Cd_w0<-cxr_pm_multifit(data = R5_Cd,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(lambda = 1,
alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = NULL,
# no standard errors
bootstrap_samples = 200)
summary(obs.R1_Cd_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4
SR1 0.007357226 NA 0.02747775
SR2 NA 0.046099244 0.02454420
SR4 -0.017843057 0.016194109 0.02532583
SR5 0.041329244 0.009208599 NA
SR5
SR1 0.01634495
SR2 0.03043861
SR4 NA
SR5 0.05166503
#summary(obs.R2_Cd_w0)
summary(obs.R3_Cd_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4
SR1 0.01183117 NA -0.005638143
SR2 NA 0.021130861 0.025735206
SR4 0.04642142 -0.026951900 0.009475739
SR5 0.04581416 0.000886977 NA
SR5
SR1 -0.006037275
SR2 0.019139983
SR4 NA
SR5 0.032574031
summary(obs.R4_Cd_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4
SR1 0.002187775 NA 0.004587916
SR2 NA 0.004657604 -0.001765893
SR4 -0.051846899 0.014164460 -0.003713025
SR5 0.063991478 -0.003949514 NA
SR5
SR1 0.01455484
SR2 -0.01844620
SR4 NA
SR5 0.03429801
summary(obs.R5_Cd_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4
SR1 0.022925211 NA 0.036058080
SR2 NA -0.014002673 0.012986026
SR4 -0.026519373 0.004816519 0.008233322
SR5 0.007813674 0.003917652 NA
SR5
SR1 -0.026508822
SR2 0.004086218
SR4 NA
SR5 0.057530995
### For replicate I can't find good initial conditions, lets do this by hand
# obs.R2_Cd_w0<-cxr_pm_multifit(data = R2_Cd,
# focal_column = my.reg[c(1,3,4)],
# model_family = "BH",
# covariates = NULL,
# optimization_method = "Nelder-Mead",
# alpha_form = "pairwise",
# lambda_cov_form = "none",
# alpha_cov_form = "none",
# initial_values = list(lambda = 1,
# alpha_intra = 0.1,
# alpha_inter = 0.1),
# fixed_terms = NULL,
# # no standard errors
# bootstrap_samples =10)
# This one works well
obs.R2_Cd_w0_sr1<-cxr_pm_fit(data = R2_Cd[[1]],
focal_column = my.reg[1],
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(lambda = 1,
alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = NULL,
# no standard errors
bootstrap_samples = 200)
#for replicate 2 we will do the fitting by hand because we may need to scale the parameters
obs.R2_Cd_w0_sr4<-cxr_pm_fit(data = R2_Cd[[2]][which(R2_Cd[[2]][,"SR1"]==0),c("fitness", "SR4")],
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(lambda = 1,
alpha_inter = 0.1),
fixed_terms = NULL,
# no standard errors
bootstrap_samples = 200)
obs.R2_Cd_w0_sr5<-cxr_pm_fit(data = R2_Cd[[3]][which(R2_Cd[[3]][,"SR1"]==0),c("fitness", "SR5")],
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(lambda = 1,
alpha_inter = 0.1),
fixed_terms = NULL,
# no standard errors
bootstrap_samples = 200)
obs.R2_Cd_w0_sr4_inter<-cxr_pm_fit(data = R2_Cd[[2]][which(R2_Cd[[2]][,"SR1"]!=0),c("fitness", "SR1")],
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list( alpha_inter = 0.1),
fixed_terms = list(lambda=obs.R2_Cd_w0_sr4$lambda),
# no standard errors
bootstrap_samples = 200)
obs.R2_Cd_w0_sr5_inter<-cxr_pm_fit(data = R2_Cd[[3]][which(R2_Cd[[3]][,"SR1"]!=0),c("fitness", "SR1")],
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list( alpha_inter = 0.1),
fixed_terms = list(lambda=obs.R2_Cd_w0_sr5$lambda),
# no standard errors
bootstrap_samples = 200)
cxr_param_w0C<-expand.grid(Tu_Regime=c("SR1", "SR2"), Te_Regime=c("SR4", "SR5"), Replicate=c(1,2,3,4,5), Environment=c("Cd"))
cxr_param_w0C$Tu_lambda<-0
cxr_param_w0C$Te_lambda<-0
cxr_param_w0C$Tu_intra<-0
cxr_param_w0C$Te_intra<-0
cxr_param_w0C$Tu_inter<-0
cxr_param_w0C$Te_inter<-0
#removing SR2 for replicate 2
cxr_param_w0C<-cxr_param_w0C[-which(cxr_param_w0C$Replicate==2 & cxr_param_w0C$Tu_Regime=="SR2"),]
cxr_param_w0C[which(cxr_param_w0C$Replicate==1),"Tu_lambda"]<-obs.R1_Cd_w0$lambda[1:2]
cxr_param_w0C[which(cxr_param_w0C$Replicate==1),"Te_lambda"]<-obs.R1_Cd_w0$lambda[c(3,3,4,4)]
cxr_param_w0C[which(cxr_param_w0C$Replicate==2),"Tu_lambda"]<-obs.R2_Cd_w0_sr1$lambda
cxr_param_w0C[which(cxr_param_w0C$Replicate==2),"Te_lambda"]<-c(obs.R2_Cd_w0_sr4$lambda, obs.R2_Cd_w0_sr5$lambda)
cxr_param_w0C[which(cxr_param_w0C$Replicate==3),"Tu_lambda"]<-obs.R3_Cd_w0$lambda[1:2]
cxr_param_w0C[which(cxr_param_w0C$Replicate==3),"Te_lambda"]<-obs.R3_Cd_w0$lambda[c(3,3,4,4)]
cxr_param_w0C[which(cxr_param_w0C$Replicate==4),"Tu_lambda"]<-obs.R4_Cd_w0$lambda[1:2]
cxr_param_w0C[which(cxr_param_w0C$Replicate==4),"Te_lambda"]<-obs.R4_Cd_w0$lambda[c(3,3,4,4)]
cxr_param_w0C[which(cxr_param_w0C$Replicate==5),"Tu_lambda"]<-obs.R5_Cd_w0$lambda[1:2]
cxr_param_w0C[which(cxr_param_w0C$Replicate==5),"Te_lambda"]<-obs.R5_Cd_w0$lambda[c(3,3,4,4)]
cxr_param_w0C[which(cxr_param_w0C$Replicate==1),"Tu_intra"]<-rep(c(obs.R1_Cd_w0$alpha_matrix[1,1], obs.R1_Cd_w0$alpha_matrix[2,2]), 2)
cxr_param_w0C[which(cxr_param_w0C$Replicate==1),"Te_intra"]<-rep(c(obs.R1_Cd_w0$alpha_matrix[3,3], obs.R1_Cd_w0$alpha_matrix[4,4]), each=2)
cxr_param_w0C[which(cxr_param_w0C$Replicate==2),"Tu_intra"]<-obs.R2_Cd_w0_sr1$alpha_intra
cxr_param_w0C[which(cxr_param_w0C$Replicate==2),"Te_intra"]<-c(obs.R2_Cd_w0_sr4$alpha_inter, obs.R2_Cd_w0_sr5$alpha_inter)
cxr_param_w0C[which(cxr_param_w0C$Replicate==3),"Tu_intra"]<-rep(c(obs.R3_Cd_w0$alpha_matrix[1,1], obs.R3_Cd_w0$alpha_matrix[2,2]), 2)
cxr_param_w0C[which(cxr_param_w0C$Replicate==3),"Te_intra"]<-rep(c(obs.R3_Cd_w0$alpha_matrix[3,3], obs.R3_Cd_w0$alpha_matrix[4,4]), each=2)
cxr_param_w0C[which(cxr_param_w0C$Replicate==4),"Tu_intra"]<-rep(c(obs.R4_Cd_w0$alpha_matrix[1,1], obs.R4_Cd_w0$alpha_matrix[2,2]), 2)
cxr_param_w0C[which(cxr_param_w0C$Replicate==4),"Te_intra"]<-rep(c(obs.R4_Cd_w0$alpha_matrix[3,3], obs.R4_Cd_w0$alpha_matrix[4,4]), each=2)
cxr_param_w0C[which(cxr_param_w0C$Replicate==5),"Tu_intra"]<-rep(c(obs.R5_Cd_w0$alpha_matrix[1,1], obs.R5_Cd_w0$alpha_matrix[2,2]), 2)
cxr_param_w0C[which(cxr_param_w0C$Replicate==5),"Te_intra"]<-rep(c(obs.R5_Cd_w0$alpha_matrix[3,3], obs.R5_Cd_w0$alpha_matrix[4,4]), each=2)
cxr_param_w0C[which(cxr_param_w0C$Replicate==1),"Tu_inter"]<-c(obs.R1_Cd_w0$alpha_matrix[1,3], obs.R1_Cd_w0$alpha_matrix[2,3],obs.R1_Cd_w0$alpha_matrix[1,4], obs.R1_Cd_w0$alpha_matrix[2,4])
cxr_param_w0C[which(cxr_param_w0C$Replicate==1),"Te_inter"]<-c(obs.R1_Cd_w0$alpha_matrix[3,1], obs.R1_Cd_w0$alpha_matrix[3,2],obs.R1_Cd_w0$alpha_matrix[4,1], obs.R1_Cd_w0$alpha_matrix[4,2])
cxr_param_w0C[which(cxr_param_w0C$Replicate==2),"Tu_inter"]<-obs.R2_Cd_w0_sr1$alpha_inter[2:3]
cxr_param_w0C[which(cxr_param_w0C$Replicate==2),"Te_inter"]<-c(obs.R2_Cd_w0_sr4_inter$alpha_inter, obs.R2_Cd_w0_sr5_inter$alpha_inter)
cxr_param_w0C[which(cxr_param_w0C$Replicate==3),"Tu_inter"]<-c(obs.R3_Cd_w0$alpha_matrix[1,3], obs.R3_Cd_w0$alpha_matrix[2,3],obs.R3_Cd_w0$alpha_matrix[1,4], obs.R3_Cd_w0$alpha_matrix[2,4])
cxr_param_w0C[which(cxr_param_w0C$Replicate==3),"Te_inter"]<-c(obs.R3_Cd_w0$alpha_matrix[3,1], obs.R3_Cd_w0$alpha_matrix[3,2],obs.R3_Cd_w0$alpha_matrix[4,1], obs.R3_Cd_w0$alpha_matrix[4,2])
cxr_param_w0C[which(cxr_param_w0C$Replicate==4),"Tu_inter"]<-c(obs.R4_Cd_w0$alpha_matrix[1,3], obs.R4_Cd_w0$alpha_matrix[2,3],obs.R4_Cd_w0$alpha_matrix[1,4], obs.R4_Cd_w0$alpha_matrix[2,4])
cxr_param_w0C[which(cxr_param_w0C$Replicate==4),"Te_inter"]<-c(obs.R4_Cd_w0$alpha_matrix[3,1], obs.R4_Cd_w0$alpha_matrix[3,2],obs.R4_Cd_w0$alpha_matrix[4,1], obs.R4_Cd_w0$alpha_matrix[4,2])
cxr_param_w0C[which(cxr_param_w0C$Replicate==5),"Tu_inter"]<-c(obs.R5_Cd_w0$alpha_matrix[1,3], obs.R5_Cd_w0$alpha_matrix[2,3],obs.R5_Cd_w0$alpha_matrix[1,4], obs.R5_Cd_w0$alpha_matrix[2,4])
cxr_param_w0C[which(cxr_param_w0C$Replicate==5),"Te_inter"]<-c(obs.R5_Cd_w0$alpha_matrix[3,1], obs.R5_Cd_w0$alpha_matrix[3,2],obs.R5_Cd_w0$alpha_matrix[4,1], obs.R5_Cd_w0$alpha_matrix[4,2])
cxr_param_w0C
### Lower
cxr_param_w0C_lower<-expand.grid(Tu_Regime=c("SR1", "SR2"), Te_Regime=c("SR4", "SR5"), Replicate=c(1,2,3,4,5), Environment=c("Cd"))
cxr_param_w0C_lower$Tu_lambda<-0
cxr_param_w0C_lower$Te_lambda<-0
cxr_param_w0C_lower$Tu_intra<-0
cxr_param_w0C_lower$Te_intra<-0
cxr_param_w0C_lower$Tu_inter<-0
cxr_param_w0C_lower$Te_inter<-0
#removing SR2 for replicate 2
cxr_param_w0C_lower<-cxr_param_w0C_lower[-which(cxr_param_w0C_lower$Replicate==2 & cxr_param_w0C_lower$Tu_Regime=="SR2"),]
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==1),"Tu_lambda"]<-rep(c(obs.R1_Cd_w0$lambda[1]-obs.R1_Cd_w0$lambda_standard_error[1], obs.R1_Cd_w0$lambda[2]-obs.R1_Cd_w0$lambda_standard_error[2]), 2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==1),"Te_lambda"]<-rep(c(obs.R1_Cd_w0$lambda[3]-obs.R1_Cd_w0$lambda_standard_error[3], obs.R1_Cd_w0$lambda[4]-obs.R1_Cd_w0$lambda_standard_error[4]), each=2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==2),"Tu_lambda"]<-c(obs.R2_Cd_w0_sr1$lambda-obs.R2_Cd_w0_sr1$lambda_standard_error)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==2),"Te_lambda"]<-c(obs.R2_Cd_w0_sr4$lambda-obs.R2_Cd_w0_sr4$lambda_standard_error, obs.R2_Cd_w0_sr5$lambda-obs.R2_Cd_w0_sr5$lambda_standard_error)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==3),"Tu_lambda"]<-rep(c(obs.R3_Cd_w0$lambda[1]-obs.R3_Cd_w0$lambda_standard_error[1], obs.R3_Cd_w0$lambda[2]-obs.R3_Cd_w0$lambda_standard_error[2]), 2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==3),"Te_lambda"]<-rep(c(obs.R3_Cd_w0$lambda[3]-obs.R3_Cd_w0$lambda_standard_error[3], obs.R3_Cd_w0$lambda[4]-obs.R3_Cd_w0$lambda_standard_error[4]), each=2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==4),"Tu_lambda"]<-rep(c(obs.R4_Cd_w0$lambda[1]-obs.R4_Cd_w0$lambda_standard_error[1], obs.R4_Cd_w0$lambda[2]-obs.R4_Cd_w0$lambda_standard_error[2]), 2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==4),"Te_lambda"]<-rep(c(obs.R4_Cd_w0$lambda[3]-obs.R4_Cd_w0$lambda_standard_error[3], obs.R4_Cd_w0$lambda[4]-obs.R4_Cd_w0$lambda_standard_error[4]), each=2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==5),"Tu_lambda"]<-rep(c(obs.R5_Cd_w0$lambda[1]-obs.R5_Cd_w0$lambda_standard_error[1], obs.R5_Cd_w0$lambda[2]-obs.R5_Cd_w0$lambda_standard_error[2]), 2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==5),"Te_lambda"]<-rep(c(obs.R5_Cd_w0$lambda[3]-obs.R5_Cd_w0$lambda_standard_error[3], obs.R5_Cd_w0$lambda[4]-obs.R5_Cd_w0$lambda_standard_error[4]), each=2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==1),"Tu_intra"]<-rep(c(obs.R1_Cd_w0$alpha_matrix[1,1]-obs.R1_Cd_w0$alpha_matrix_standard_error[1,1], obs.R1_Cd_w0$alpha_matrix[2,2]-obs.R1_Cd_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==1),"Te_intra"]<-rep(c(obs.R1_Cd_w0$alpha_matrix[3,3]-obs.R1_Cd_w0$alpha_matrix_standard_error[3,3], obs.R1_Cd_w0$alpha_matrix[4,4]-obs.R1_Cd_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==2),"Tu_intra"]<-obs.R2_Cd_w0_sr1$alpha_intra-obs.R2_Cd_w0_sr1$alpha_intra_standard_error
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==2),"Te_intra"]<-c(obs.R2_Cd_w0_sr4$alpha_inter-obs.R2_Cd_w0_sr4$alpha_inter_standard_error, obs.R2_Cd_w0_sr5$alpha_inter-obs.R2_Cd_w0_sr5$alpha_inter_standard_error)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==3),"Tu_intra"]<-rep(c(obs.R3_Cd_w0$alpha_matrix[1,1]-obs.R3_Cd_w0$alpha_matrix_standard_error[1,1], obs.R3_Cd_w0$alpha_matrix[2,2]-obs.R3_Cd_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==3),"Te_intra"]<-rep(c(obs.R3_Cd_w0$alpha_matrix[3,3]-obs.R3_Cd_w0$alpha_matrix_standard_error[3,3], obs.R3_Cd_w0$alpha_matrix[4,4]-obs.R3_Cd_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==4),"Tu_intra"]<-rep(c(obs.R4_Cd_w0$alpha_matrix[1,1]-obs.R4_Cd_w0$alpha_matrix_standard_error[1,1], obs.R4_Cd_w0$alpha_matrix[2,2]-obs.R4_Cd_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==4),"Te_intra"]<-rep(c(obs.R4_Cd_w0$alpha_matrix[3,3]-obs.R4_Cd_w0$alpha_matrix_standard_error[3,3], obs.R4_Cd_w0$alpha_matrix[4,4]-obs.R4_Cd_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==5),"Tu_intra"]<-rep(c(obs.R5_Cd_w0$alpha_matrix[1,1]-obs.R5_Cd_w0$alpha_matrix_standard_error[1,1], obs.R5_Cd_w0$alpha_matrix[2,2]-obs.R5_Cd_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==5),"Te_intra"]<-rep(c(obs.R5_Cd_w0$alpha_matrix[3,3]-obs.R5_Cd_w0$alpha_matrix_standard_error[3,3], obs.R5_Cd_w0$alpha_matrix[4,4]-obs.R5_Cd_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==1),"Tu_inter"]<-c(obs.R1_Cd_w0$alpha_matrix[1,3]-obs.R1_Cd_w0$alpha_matrix_standard_error[1,3], obs.R1_Cd_w0$alpha_matrix[2,3]-obs.R1_Cd_w0$alpha_matrix_standard_error[2,3],obs.R1_Cd_w0$alpha_matrix[1,4]-obs.R1_Cd_w0$alpha_matrix_standard_error[1,4], obs.R1_Cd_w0$alpha_matrix[2,4]-obs.R1_Cd_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==1),"Te_inter"]<-c(obs.R1_Cd_w0$alpha_matrix[3,1]-obs.R1_Cd_w0$alpha_matrix_standard_error[3,1], obs.R1_Cd_w0$alpha_matrix[3,2]-obs.R1_Cd_w0$alpha_matrix_standard_error[3,2],obs.R1_Cd_w0$alpha_matrix[4,1]-obs.R1_Cd_w0$alpha_matrix_standard_error[4,1], obs.R1_Cd_w0$alpha_matrix[4,2]-obs.R1_Cd_w0$alpha_matrix_standard_error[4,2])
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==2),"Tu_inter"]<-obs.R2_Cd_w0_sr1$alpha_inter[2:3]-obs.R2_Cd_w0_sr1$alpha_inter_standard_error[2:3]
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==2),"Te_inter"]<-c(obs.R2_Cd_w0_sr4_inter$alpha_inter-obs.R2_Cd_w0_sr4_inter$alpha_inter_standard_error, obs.R2_Cd_w0_sr5_inter$alpha_inter-obs.R2_Cd_w0_sr5_inter$alpha_inter_standard_error)
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==3),"Tu_inter"]<-c(obs.R3_Cd_w0$alpha_matrix[1,3]-obs.R3_Cd_w0$alpha_matrix_standard_error[1,3], obs.R3_Cd_w0$alpha_matrix[2,3]-obs.R3_Cd_w0$alpha_matrix_standard_error[2,3],obs.R3_Cd_w0$alpha_matrix[1,4]-obs.R3_Cd_w0$alpha_matrix_standard_error[1,4], obs.R3_Cd_w0$alpha_matrix[2,4]-obs.R3_Cd_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==3),"Te_inter"]<-c(obs.R3_Cd_w0$alpha_matrix[3,1]-obs.R3_Cd_w0$alpha_matrix_standard_error[3,1], obs.R3_Cd_w0$alpha_matrix[3,2]-obs.R3_Cd_w0$alpha_matrix_standard_error[3,2],obs.R3_Cd_w0$alpha_matrix[4,1]-obs.R3_Cd_w0$alpha_matrix_standard_error[4,1], obs.R3_Cd_w0$alpha_matrix[4,2]-obs.R3_Cd_w0$alpha_matrix_standard_error[4,2])
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==4),"Tu_inter"]<-c(obs.R4_Cd_w0$alpha_matrix[1,3]-obs.R4_Cd_w0$alpha_matrix_standard_error[1,3], obs.R4_Cd_w0$alpha_matrix[2,3]-obs.R4_Cd_w0$alpha_matrix_standard_error[2,3],obs.R4_Cd_w0$alpha_matrix[1,4]-obs.R4_Cd_w0$alpha_matrix_standard_error[1,4], obs.R4_Cd_w0$alpha_matrix[2,4]-obs.R4_Cd_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==4),"Te_inter"]<-c(obs.R4_Cd_w0$alpha_matrix[3,1]-obs.R4_Cd_w0$alpha_matrix_standard_error[3,1], obs.R4_Cd_w0$alpha_matrix[3,2]-obs.R4_Cd_w0$alpha_matrix_standard_error[3,2],obs.R4_Cd_w0$alpha_matrix[4,1]-obs.R4_Cd_w0$alpha_matrix_standard_error[4,1], obs.R4_Cd_w0$alpha_matrix[4,2]-obs.R4_Cd_w0$alpha_matrix_standard_error[4,2])
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==5),"Tu_inter"]<-c(obs.R5_Cd_w0$alpha_matrix[1,3]-obs.R5_Cd_w0$alpha_matrix_standard_error[1,3], obs.R5_Cd_w0$alpha_matrix[2,3]-obs.R5_Cd_w0$alpha_matrix_standard_error[2,3],obs.R5_Cd_w0$alpha_matrix[1,4]-obs.R5_Cd_w0$alpha_matrix_standard_error[1,4], obs.R5_Cd_w0$alpha_matrix[2,4]-obs.R5_Cd_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0C_lower[which(cxr_param_w0C_lower$Replicate==5),"Te_inter"]<-c(obs.R5_Cd_w0$alpha_matrix[3,1]-obs.R5_Cd_w0$alpha_matrix_standard_error[3,1], obs.R5_Cd_w0$alpha_matrix[3,2]-obs.R5_Cd_w0$alpha_matrix_standard_error[3,2],obs.R5_Cd_w0$alpha_matrix[4,1]-obs.R5_Cd_w0$alpha_matrix_standard_error[4,1], obs.R5_Cd_w0$alpha_matrix[4,2]-obs.R5_Cd_w0$alpha_matrix_standard_error[4,2])
### upper
cxr_param_w0C_upper<-expand.grid(Tu_Regime=c("SR1", "SR2"), Te_Regime=c("SR4", "SR5"), Replicate=c(1,2,3,4,5), Environment=c("Cd"))
cxr_param_w0C_upper$Tu_lambda<-0
cxr_param_w0C_upper$Te_lambda<-0
cxr_param_w0C_upper$Tu_intra<-0
cxr_param_w0C_upper$Te_intra<-0
cxr_param_w0C_upper$Tu_inter<-0
cxr_param_w0C_upper$Te_inter<-0
#removing SR2 for replicate 2
cxr_param_w0C_upper<-cxr_param_w0C_upper[-which(cxr_param_w0C_upper$Replicate==2 & cxr_param_w0C_upper$Tu_Regime=="SR2"),]
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==1),"Tu_lambda"]<-rep(c(obs.R1_Cd_w0$lambda[1]+obs.R1_Cd_w0$lambda_standard_error[1], obs.R1_Cd_w0$lambda[2]+obs.R1_Cd_w0$lambda_standard_error[2]), 2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==1),"Te_lambda"]<-rep(c(obs.R1_Cd_w0$lambda[3]+obs.R1_Cd_w0$lambda_standard_error[3], obs.R1_Cd_w0$lambda[4]+obs.R1_Cd_w0$lambda_standard_error[4]), each=2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==2),"Tu_lambda"]<-c(obs.R2_Cd_w0_sr1$lambda+obs.R2_Cd_w0_sr1$lambda_standard_error)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==2),"Te_lambda"]<-c(obs.R2_Cd_w0_sr4$lambda+obs.R2_Cd_w0_sr4$lambda_standard_error, obs.R2_Cd_w0_sr5$lambda+obs.R2_Cd_w0_sr5$lambda_standard_error)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==3),"Tu_lambda"]<-rep(c(obs.R3_Cd_w0$lambda[1]+obs.R3_Cd_w0$lambda_standard_error[1], obs.R3_Cd_w0$lambda[2]+obs.R3_Cd_w0$lambda_standard_error[2]), 2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==3),"Te_lambda"]<-rep(c(obs.R3_Cd_w0$lambda[3]+obs.R3_Cd_w0$lambda_standard_error[3], obs.R3_Cd_w0$lambda[4]+obs.R3_Cd_w0$lambda_standard_error[4]), each=2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==4),"Tu_lambda"]<-rep(c(obs.R4_Cd_w0$lambda[1]+obs.R4_Cd_w0$lambda_standard_error[1], obs.R4_Cd_w0$lambda[2]+obs.R4_Cd_w0$lambda_standard_error[2]), 2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==4),"Te_lambda"]<-rep(c(obs.R4_Cd_w0$lambda[3]+obs.R4_Cd_w0$lambda_standard_error[3], obs.R4_Cd_w0$lambda[4]+obs.R4_Cd_w0$lambda_standard_error[4]), each=2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==5),"Tu_lambda"]<-rep(c(obs.R5_Cd_w0$lambda[1]+obs.R5_Cd_w0$lambda_standard_error[1], obs.R5_Cd_w0$lambda[2]+obs.R5_Cd_w0$lambda_standard_error[2]), 2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==5),"Te_lambda"]<-rep(c(obs.R5_Cd_w0$lambda[3]+obs.R5_Cd_w0$lambda_standard_error[3], obs.R5_Cd_w0$lambda[4]+obs.R5_Cd_w0$lambda_standard_error[4]), each=2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==1),"Tu_intra"]<-rep(c(obs.R1_Cd_w0$alpha_matrix[1,1]+obs.R1_Cd_w0$alpha_matrix_standard_error[1,1], obs.R1_Cd_w0$alpha_matrix[2,2]+obs.R1_Cd_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==1),"Te_intra"]<-rep(c(obs.R1_Cd_w0$alpha_matrix[3,3]+obs.R1_Cd_w0$alpha_matrix_standard_error[3,3], obs.R1_Cd_w0$alpha_matrix[4,4]+obs.R1_Cd_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==2),"Tu_intra"]<-obs.R2_Cd_w0_sr1$alpha_intra + obs.R2_Cd_w0_sr1$alpha_intra_standard_error
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==2),"Te_intra"]<-c(obs.R2_Cd_w0_sr4$alpha_inter+obs.R2_Cd_w0_sr4$alpha_inter_standard_error, obs.R2_Cd_w0_sr5$alpha_inter+obs.R2_Cd_w0_sr5$alpha_inter_standard_error)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==3),"Tu_intra"]<-rep(c(obs.R3_Cd_w0$alpha_matrix[1,1]+obs.R3_Cd_w0$alpha_matrix_standard_error[1,1], obs.R3_Cd_w0$alpha_matrix[2,2]+obs.R3_Cd_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==3),"Te_intra"]<-rep(c(obs.R3_Cd_w0$alpha_matrix[3,3]+obs.R3_Cd_w0$alpha_matrix_standard_error[3,3], obs.R3_Cd_w0$alpha_matrix[4,4]+obs.R3_Cd_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==4),"Tu_intra"]<-rep(c(obs.R4_Cd_w0$alpha_matrix[1,1]+obs.R4_Cd_w0$alpha_matrix_standard_error[1,1], obs.R4_Cd_w0$alpha_matrix[2,2]+obs.R4_Cd_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==4),"Te_intra"]<-rep(c(obs.R4_Cd_w0$alpha_matrix[3,3]+obs.R4_Cd_w0$alpha_matrix_standard_error[3,3], obs.R4_Cd_w0$alpha_matrix[4,4]+obs.R4_Cd_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==5),"Tu_intra"]<-rep(c(obs.R5_Cd_w0$alpha_matrix[1,1]+obs.R5_Cd_w0$alpha_matrix_standard_error[1,1], obs.R5_Cd_w0$alpha_matrix[2,2]+obs.R5_Cd_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==5),"Te_intra"]<-rep(c(obs.R5_Cd_w0$alpha_matrix[3,3]+obs.R5_Cd_w0$alpha_matrix_standard_error[3,3], obs.R5_Cd_w0$alpha_matrix[4,4]+obs.R5_Cd_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==1),"Tu_inter"]<-c(obs.R1_Cd_w0$alpha_matrix[1,3]+obs.R1_Cd_w0$alpha_matrix_standard_error[1,3], obs.R1_Cd_w0$alpha_matrix[2,3]+obs.R1_Cd_w0$alpha_matrix_standard_error[2,3],obs.R1_Cd_w0$alpha_matrix[1,4]+obs.R1_Cd_w0$alpha_matrix_standard_error[1,4], obs.R1_Cd_w0$alpha_matrix[2,4]+obs.R1_Cd_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==1),"Te_inter"]<-c(obs.R1_Cd_w0$alpha_matrix[3,1]+obs.R1_Cd_w0$alpha_matrix_standard_error[3,1], obs.R1_Cd_w0$alpha_matrix[3,2]+obs.R1_Cd_w0$alpha_matrix_standard_error[3,2],obs.R1_Cd_w0$alpha_matrix[4,1]+obs.R1_Cd_w0$alpha_matrix_standard_error[4,1], obs.R1_Cd_w0$alpha_matrix[4,2]+obs.R1_Cd_w0$alpha_matrix_standard_error[4,2])
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==2),"Tu_inter"]<-obs.R2_Cd_w0_sr1$alpha_inter[2:3]+obs.R2_Cd_w0_sr1$alpha_inter_standard_error[2:3]
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==2),"Te_inter"]<-c(obs.R2_Cd_w0_sr4_inter$alpha_inter+obs.R2_Cd_w0_sr4_inter$alpha_inter_standard_error, obs.R2_Cd_w0_sr5_inter$alpha_inter+obs.R2_Cd_w0_sr5_inter$alpha_inter_standard_error)
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==3),"Tu_inter"]<-c(obs.R3_Cd_w0$alpha_matrix[1,3]+obs.R3_Cd_w0$alpha_matrix_standard_error[1,3], obs.R3_Cd_w0$alpha_matrix[2,3]+obs.R3_Cd_w0$alpha_matrix_standard_error[2,3],obs.R3_Cd_w0$alpha_matrix[1,4]+obs.R3_Cd_w0$alpha_matrix_standard_error[1,4], obs.R3_Cd_w0$alpha_matrix[2,4]+obs.R3_Cd_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==3),"Te_inter"]<-c(obs.R3_Cd_w0$alpha_matrix[3,1]+obs.R3_Cd_w0$alpha_matrix_standard_error[3,1], obs.R3_Cd_w0$alpha_matrix[3,2]+obs.R3_Cd_w0$alpha_matrix_standard_error[3,2],obs.R3_Cd_w0$alpha_matrix[4,1]+obs.R3_Cd_w0$alpha_matrix_standard_error[4,1], obs.R3_Cd_w0$alpha_matrix[4,2]+obs.R3_Cd_w0$alpha_matrix_standard_error[4,2])
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==4),"Tu_inter"]<-c(obs.R4_Cd_w0$alpha_matrix[1,3]+obs.R4_Cd_w0$alpha_matrix_standard_error[1,3], obs.R4_Cd_w0$alpha_matrix[2,3]+obs.R4_Cd_w0$alpha_matrix_standard_error[2,3],obs.R4_Cd_w0$alpha_matrix[1,4]+obs.R4_Cd_w0$alpha_matrix_standard_error[1,4], obs.R4_Cd_w0$alpha_matrix[2,4]+obs.R4_Cd_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==4),"Te_inter"]<-c(obs.R4_Cd_w0$alpha_matrix[3,1]+obs.R4_Cd_w0$alpha_matrix_standard_error[3,1], obs.R4_Cd_w0$alpha_matrix[3,2]+obs.R4_Cd_w0$alpha_matrix_standard_error[3,2],obs.R4_Cd_w0$alpha_matrix[4,1]+obs.R4_Cd_w0$alpha_matrix_standard_error[4,1], obs.R4_Cd_w0$alpha_matrix[4,2]+obs.R4_Cd_w0$alpha_matrix_standard_error[4,2])
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==5),"Tu_inter"]<-c(obs.R5_Cd_w0$alpha_matrix[1,3]+obs.R5_Cd_w0$alpha_matrix_standard_error[1,3], obs.R5_Cd_w0$alpha_matrix[2,3]+obs.R5_Cd_w0$alpha_matrix_standard_error[2,3],obs.R5_Cd_w0$alpha_matrix[1,4]+obs.R5_Cd_w0$alpha_matrix_standard_error[1,4], obs.R5_Cd_w0$alpha_matrix[2,4]+obs.R5_Cd_w0$alpha_matrix_standard_error[2,4])
cxr_param_w0C_upper[which(cxr_param_w0C_upper$Replicate==5),"Te_inter"]<-c(obs.R5_Cd_w0$alpha_matrix[3,1]+obs.R5_Cd_w0$alpha_matrix_standard_error[3,1], obs.R5_Cd_w0$alpha_matrix[3,2]+obs.R5_Cd_w0$alpha_matrix_standard_error[3,2],obs.R5_Cd_w0$alpha_matrix[4,1]+obs.R5_Cd_w0$alpha_matrix_standard_error[4,1], obs.R5_Cd_w0$alpha_matrix[4,2]+obs.R5_Cd_w0$alpha_matrix_standard_error[4,2])
param_all_w0<-as.data.frame(rbind(cxr_param_w0, cxr_param_w0C))
param_all_w0_lower<-as.data.frame(rbind(cxr_param_w0_lower, cxr_param_w0C_lower))
param_all_w0_upper<-as.data.frame(rbind(cxr_param_w0_upper, cxr_param_w0C_upper))
param_all_w0_lower
param_all_w0_upper
write.csv(param_all_w0, "./NewAnalyses/cxr_normal/parameters_cxr_normal.csv")
write.csv(param_all_w0_upper, "./NewAnalyses/cxr_normal/parameters_cxr_normal_upper.csv")
write.csv(param_all_w0_lower, "./NewAnalyses/cxr_normal/parameters_cxr_normal_lower.csv")
param_all_w0<-read.csv("./NewAnalyses/cxr_normal/parameters_cxr_normal.csv")
param_all_w0_upper<-read.csv("./NewAnalyses/cxr_normal/parameters_cxr_normal_upper.csv")
param_all_w0_lower<-read.csv("./NewAnalyses/cxr_normal/parameters_cxr_normal_lower.csv")
param_all_w0<-param_all_w0[,-1]
param_all_w0_upper<-param_all_w0_upper[,-1]
param_all_w0_lower<-param_all_w0_lower[,-1]
str(param_all_w0)
'data.frame': 36 obs. of 10 variables:
$ Tu_Regime : chr "SR1" "SR2" "SR1" "SR2" ...
$ Te_Regime : chr "SR4" "SR4" "SR5" "SR5" ...
$ Replicate : int 1 1 1 1 2 2 3 3 3 3 ...
$ Environment: chr "N" "N" "N" "N" ...
$ Tu_lambda : num 2.79 2.07 2.79 2.07 2.05 ...
$ Te_lambda : num 4.84 4.84 5.07 5.07 5.12 ...
$ Tu_intra : num 0.06227 -0.00636 0.06227 -0.00636 -0.00608 ...
$ Te_intra : num 0.0213 0.0213 0.0259 0.0259 -0.0236 ...
$ Tu_inter : num 0.0211 0.0165 0.0406 0.0166 -0.0224 ...
$ Te_inter : num 0.0851 0.0548 0.0432 0.1002 0.1675 ...
str(param_all_w0_upper)
'data.frame': 36 obs. of 10 variables:
$ Tu_Regime : chr "SR1" "SR2" "SR1" "SR2" ...
$ Te_Regime : chr "SR4" "SR4" "SR5" "SR5" ...
$ Replicate : int 1 1 1 1 2 2 3 3 3 3 ...
$ Environment: chr "N" "N" "N" "N" ...
$ Tu_lambda : num 3.18 2.35 3.18 2.35 2.32 ...
$ Te_lambda : num 5.84 5.84 6.07 6.07 6.76 ...
$ Tu_intra : num 0.0857 0.016 0.0857 0.016 0.0223 ...
$ Te_intra : num 0.062 0.062 0.0631 0.0631 0.0246 ...
$ Tu_inter : num 0.05931 0.04554 0.06747 0.0462 0.00534 ...
$ Te_inter : num 0.1309 0.0928 0.0879 0.1446 0.3441 ...
str(param_all_w0_lower)
'data.frame': 36 obs. of 10 variables:
$ Tu_Regime : chr "SR1" "SR2" "SR1" "SR2" ...
$ Te_Regime : chr "SR4" "SR4" "SR5" "SR5" ...
$ Replicate : int 1 1 1 1 2 2 3 3 3 3 ...
$ Environment: chr "N" "N" "N" "N" ...
$ Tu_lambda : num 2.4 1.8 2.4 1.8 1.79 ...
$ Te_lambda : num 3.85 3.85 4.07 4.07 3.48 ...
$ Tu_intra : num 0.0388 -0.0287 0.0388 -0.0287 -0.0345 ...
$ Te_intra : num -0.0194 -0.0194 -0.0113 -0.0113 -0.0718 ...
$ Tu_inter : num -0.0171 -0.0126 0.0137 -0.0129 -0.0501 ...
$ Te_inter : num 0.03931 0.01684 -0.00151 0.05585 -0.00903 ...
param_all_w0_long<-gather(param_all_w0, parameter, value,Tu_lambda:Te_inter )
param_all_w0_long$category<-mapvalues(param_all_w0_long$parameter, c("Tu_lambda", "Te_lambda", "Tu_intra", "Te_intra","Tu_inter", "Te_inter"), c("lambda", "lambda", "intra", "intra", "inter", "inter"))
param_all_w0_lower_long<-gather(param_all_w0_lower, parameter, value,Tu_lambda:Te_inter )
param_all_w0_lower_long$category<-mapvalues(param_all_w0_lower_long$parameter, c("Tu_lambda", "Te_lambda", "Tu_intra", "Te_intra","Tu_inter", "Te_inter"), c("lambda", "lambda", "intra", "intra", "inter", "inter"))
param_all_w0_upper_long<-gather(param_all_w0_upper, parameter, value,Tu_lambda:Te_inter )
param_all_w0_upper_long$category<-mapvalues(param_all_w0_upper_long$parameter, c("Tu_lambda", "Te_lambda", "Tu_intra", "Te_intra","Tu_inter", "Te_inter"), c("lambda", "lambda", "intra", "intra", "inter", "inter"))
colnames(param_all_w0_lower_long)[6]<-"lower"
colnames(param_all_w0_upper_long)[6]<-"upper"
str(param_all_w0_long)
'data.frame': 216 obs. of 7 variables:
$ Tu_Regime : chr "SR1" "SR2" "SR1" "SR2" ...
$ Te_Regime : chr "SR4" "SR4" "SR5" "SR5" ...
$ Replicate : int 1 1 1 1 2 2 3 3 3 3 ...
$ Environment: chr "N" "N" "N" "N" ...
$ parameter : chr "Tu_lambda" "Tu_lambda" "Tu_lambda" "Tu_lambda" ...
$ value : num 2.79 2.07 2.79 2.07 2.05 ...
$ category : chr "lambda" "lambda" "lambda" "lambda" ...
param_all_w0_long<-cbind(param_all_w0_long[,1:7],param_all_w0_lower_long$lower, param_all_w0_upper_long$upper)
colnames(param_all_w0_long)[8:9]<-c("lower","upper")
ggplot(subset(param_all_w0_long, parameter=="Te_intra" & Tu_Regime=="SR1"), aes(x=Te_Regime, y=value, fill=Environment))+
geom_boxplot( position=position_dodge2(0.5), outlier.alpha = 99)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("Evolved \nwithout cadmium", "Evolved \nwith cadmium"))+
scale_fill_manual(values=c("#D55E00", "#009E73"), labels=c("Water", "Cadmium"))+
scale_colour_manual(values=c("#D55E00", "#009E73"), labels=c("Water", "Cadmium"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Selection regime")
save_plot("./NewAnalyses/cxr_normal/param_RK_intraTe_w0_rep.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Tu_intra" & Te_Regime=="SR4"), aes(x=Tu_Regime, y=value, fill=Environment))+
geom_boxplot(position=position_dodge2(0.5), outlier.alpha = 99)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("Evolved \nwithout cadmium", "Evolved \nwith cadmium"))+
scale_fill_manual(values=c("#D55E00", "#009E73"), labels=c("Water", "Cadmium"))+
scale_colour_manual(values=c("#D55E00", "#009E73"), labels=c("Water", "Cadmium"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Selection regime")
save_plot("./NewAnalyses/cxr_normal/param_RK_intraTu_w0_rep.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Te_intra" & Tu_Regime=="SR1" & Environment=="Cd"), aes(x=Te_Regime, y=value, fill=Te_Regime))+
geom_boxplot(position=position_dodge2(0.5), outlier.alpha = 99)+
geom_point(alpha=0.85, position=position_dodge2(0.5))+
theme_ines+
scale_fill_manual(values=c("#D55E00", "#009E73"))+
scale_x_discrete(labels=c("Evolved \nwithout cadmium", "Evolved \nwith cadmium"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Selection regime")
save_plot("./NewAnalyses/cxr_normal/param_RK_intraTe_w0_rep_evol.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Tu_intra" & Te_Regime=="SR4" & Environment=="Cd"), aes(x=Tu_Regime, y=value, fill=Tu_Regime))+
geom_boxplot( position=position_dodge2(0.5), outlier.alpha = 99)+
geom_point(alpha=0.85, position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("Evolved \nwithout cadmium", "Evolved \nwith cadmium"))+
scale_fill_manual(values=c("#D55E00", "#009E73"))+
scale_colour_manual(values=c("#D55E00", "#009E73"), labels=c("Water", "Cadmium"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Selection regime")
save_plot("./NewAnalyses/cxr_normal/param_RK_intraTu_w0_rep_evol.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Te_intra" & Tu_Regime=="SR1"), aes(x=Environment, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Homogeneous"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Homogeneous"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_normal/param_RK_intraTe_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Tu_intra" & Te_Regime=="SR4"), aes(fill=Tu_Regime, y=value, x=Environment))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Homogeneous"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Homogeneous"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_normal/param_RK_intraTu_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Te_inter"), aes(x=Environment, y=value, fill=Te_Regime))+
facet_grid(Tu_Regime~., labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Homogeneous"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Homogeneous"))+
ylab("Interspecific competition (T. evansi)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_normal/param_RK_interTe_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Tu_inter"), aes(fill=Tu_Regime, y=value, x=Environment))+
facet_grid(Te_Regime~., labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot(outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.75))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Homogeneous"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Homogeneous"))+
ylab("Interspecific competition (T. urticae)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_normal/param_RK_interTu_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Te_lambda" & Tu_Regime=="SR1"), aes(x=Environment, y=value, fill=Te_Regime))+
geom_boxplot( position=position_dodge2(0.5), outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Homogeneous"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Homogeneous"))+
ylab("Intrinsic growth rate (T. evansi)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_normal/param_RK_lambdaTe_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Tu_lambda" & Te_Regime=="SR4"), aes(fill=Tu_Regime, y=value, x=Environment))+
geom_boxplot(position=position_dodge2(0.5), outlier.alpha =0.01)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Homogeneous"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Homogeneous"))+
ylab("Intrinsic growth rate (T. urticae)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_normal/param_RK_lambdaTu_w0_rep_env.pdf", width=20, height=15)
###########
ggplot(subset(param_all_w0_long, parameter=="Te_intra" & Tu_Regime=="SR1" & Environment=="Cd"), aes(x=Environment, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Environment")+
ylim(c(0.03,0.27))
save_plot("./NewAnalyses/cxr_normal/param_RK_intraTe_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Te_intra" & Tu_Regime=="SR1" & Environment=="N"), aes(x=Environment, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Environment")+
ylim(c(0.03,0.27))
save_plot("./NewAnalyses/cxr_normal/param_RK_intraTe_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Tu_intra" & Te_Regime=="SR4" & Environment=="Cd"), aes(fill=Tu_Regime, y=value, x=Environment))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Environment")+
ylim(c(0.03,0.27))
save_plot("./NewAnalyses/cxr_normal/param_RK_intraTu_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Tu_intra" & Te_Regime=="SR4" & Environment=="N"), aes(fill=Tu_Regime, y=value, x=Environment))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Environment")+
ylim(c(0.03,0.27))
save_plot("./NewAnalyses/cxr_normal/param_RK_intraTu_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Te_inter" & Environment=="Cd"), aes(x=Environment, y=value, fill=Te_Regime))+
facet_grid(.~Tu_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. evansi)")+
xlab("Environment")+
ylim(c(-0.05, 0.16))
save_plot("./NewAnalyses/cxr_normal/param_RK_interTe_w0_rep_Cd.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Te_inter" & Environment=="N"), aes(x=Environment, y=value, fill=Te_Regime))+
facet_grid(.~Tu_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. evansi)")+
xlab("Environment")+
ylim(c(-0.05, 0.16))
save_plot("./NewAnalyses/cxr_normal/param_RK_interTe_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Tu_inter" & Environment=="Cd"), aes(fill=Tu_Regime, y=value, x=Environment))+
facet_grid(.~Te_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot(outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.75))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. urticae)")+
xlab("Environment")+
ylim(c(-0.05, 0.12))
save_plot("./NewAnalyses/cxr_normal/param_RK_interTu_w0_rep_Cd.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Tu_inter" & Environment=="N"), aes(fill=Tu_Regime, y=value, x=Environment))+
facet_grid(.~Te_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot(outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.75))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. urticae)")+
xlab("Environment")+
ylim(c(-0.05, 0.12))
save_plot("./NewAnalyses/cxr_normal/param_RK_interTu_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Te_lambda" & Tu_Regime=="SR1" & Environment=="Cd"), aes(x=Environment, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. evansi)")+
xlab("Environment")+
ylim(c(0, 3))
save_plot("./NewAnalyses/cxr_normal/param_RK_lambdaTe_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Te_lambda" & Tu_Regime=="SR1" & Environment=="N"), aes(x=Environment, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. evansi)")+
xlab("Environment")+
ylim(c(0, 8))
save_plot("./NewAnalyses/cxr_normal/param_RK_lambdaTe_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Tu_lambda" & Te_Regime=="SR4" & Environment=="Cd"), aes(fill=Tu_Regime, y=value, x=Environment))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c( "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. urticae)")+
xlab("Environment")+
ylim(c(0, 3))
save_plot("./NewAnalyses/cxr_normal/param_RK_lambdaTu_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(param_all_w0_long, parameter=="Tu_lambda" & Te_Regime=="SR4" & Environment=="N"), aes(fill=Tu_Regime, y=value, x=Environment))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. urticae)")+
xlab("Environment")+
ylim(c(0, 8))
save_plot("./NewAnalyses/cxr_normal/param_RK_lambdaTu_w0_rep_N.pdf", width=20, height=15)
NA
NA
density_aux<-seq(0, 10, by=(10/100))
pred_df_cxr<-as.data.frame(expand_grid(Density=density_aux, Tu_Regime=c("SR1","SR2"), Te_Regime=c("SR4","SR5"), Replicate=c(1:5), Environment=c("N", "Cd")))
pred_df_cxr$Tu_mean_intra<-sapply(c(1:length(pred_df_cxr[,1])), function(x){
alpha_i<-subset(param_all_w0, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_w0, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_w0, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr$Density[x])
pred
})
pred_df_cxr$Tu_mean_inter<-sapply(c(1:length(pred_df_cxr[,1])), function(x){
alpha_i<-subset(param_all_w0, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_w0, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_w0, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr$Density[x])
pred
})
pred_df_cxr$Tu_intra_L<-sapply(c(1:length(pred_df_cxr[,1])), function(x){
alpha_i<-subset(param_all_w0_lower, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_w0_lower, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_w0_lower, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr$Density[x])
pred
})
pred_df_cxr$Tu_inter_L<-sapply(c(1:length(pred_df_cxr[,1])), function(x){
alpha_i<-subset(param_all_w0_lower, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_w0_lower, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_w0_lower, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr$Density[x])
pred
})
pred_df_cxr$Tu_intra_U<-sapply(c(1:length(pred_df_cxr[,1])), function(x){
alpha_i<-subset(param_all_w0_upper, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_w0_upper, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_w0_upper, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr$Density[x])
pred
})
pred_df_cxr$Tu_inter_U<-sapply(c(1:length(pred_df_cxr[,1])), function(x){
alpha_i<-subset(param_all_w0_upper, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_w0_upper, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_w0_upper, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr$Density[x])
pred
})
pred_df_cxr$Te_mean_intra<-sapply(c(1:length(pred_df_cxr[,1])), function(x){
alpha_i<-subset(param_all_w0, Environment==pred_df_cxr$Environment[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_w0, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_w0, Environment==pred_df_cxr$Environment[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr$Density[x])
pred
})
pred_df_cxr$Te_mean_inter<-sapply(c(1:length(pred_df_cxr[,1])), function(x){
alpha_i<-subset(param_all_w0, Environment==pred_df_cxr$Environment[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_w0, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_w0, Environment==pred_df_cxr$Environment[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr$Density[x])
pred
})
pred_df_cxr$Te_intra_L<-sapply(c(1:length(pred_df_cxr[,1])), function(x){
alpha_i<-subset(param_all_w0_lower, Environment==pred_df_cxr$Environment[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_w0_lower, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_w0_lower, Environment==pred_df_cxr$Environment[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr$Density[x])
pred
})
pred_df_cxr$Te_inter_L<-sapply(c(1:length(pred_df_cxr[,1])), function(x){
alpha_i<-subset(param_all_w0_lower, Environment==pred_df_cxr$Environment[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_w0_lower, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_w0_lower, Environment==pred_df_cxr$Environment[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr$Density[x])
pred
})
pred_df_cxr$Te_intra_U<-sapply(c(1:length(pred_df_cxr[,1])), function(x){
alpha_i<-subset(param_all_w0_upper, Environment==pred_df_cxr$Environment[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_w0_upper, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_w0_upper, Environment==pred_df_cxr$Environment[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr$Density[x])
pred
})
pred_df_cxr$Te_inter_U<-sapply(c(1:length(pred_df_cxr[,1])), function(x){
alpha_i<-subset(param_all_w0_upper, Environment==pred_df_cxr$Environment[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_w0_upper, Environment==pred_df_cxr$Environment[x] & Tu_Regime==pred_df_cxr$Tu_Regime[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_w0_upper, Environment==pred_df_cxr$Environment[x] & Te_Regime==pred_df_cxr$Te_Regime[x] & Replicate==pred_df_cxr$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr$Density[x])
pred
})
# Removing Tu evolved replicate 2 because there is no data
pred_df_cxr<-pred_df_cxr[-which(pred_df_cxr$Tu_Regime=="SR2" & pred_df_cxr$Replicate==2),]
# Transforming everything bellow 0 into 0 for the lower interval
pred_df_cxr$Te_inter_L[which(pred_df_cxr$Te_inter_L<0)]<-0
pred_df_cxr$Te_intra_L[which(pred_df_cxr$Te_intra_L<0)]<-0
pred_df_cxr$Tu_inter_L[which(pred_df_cxr$Tu_inter_L<0)]<-0
pred_df_cxr$Tu_intra_L[which(pred_df_cxr$Tu_intra_L<0)]<-0
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Tu_Regime=="SR1" & Environment=="N" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Tu_Regime=="SR1" & Environment=="N" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific control environment")
ca$Replicate<-as.character(ca$Rep)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Tu_Regime=="SR1" & Environment=="N" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Tu_Regime=="SR1" & Environment=="N" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific control environment")
save_plot("./NewAnalyses/cxr_normal/pred_Tu_INTRA_Control_water.pdf", width=25, height=10)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Tu_Regime==1 & Environment=="C" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Tu_Regime==1 & Environment=="C" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific cadmium environment")
save_plot("./NewAnalyses/cxr_normal/pred_Tu_INTRA_Control_cadmium.pdf", width=25, height=10)
#subset(pred_df_cxr, Tu_Regime==1 & Environment=="C" & Replicate==3)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Te_Regime==4 & Environment=="N" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Te_Regime==4 & Environment=="N" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="N" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. evansi")+
ggtitle("Te control intraspecific control environment")
save_plot("./NewAnalyses/cxr_normal/pred_Te_INTRA_Control_control.pdf", width=25, height=10)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Te_Regime==4 & Environment=="C" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Te_Regime==4 & Environment=="C" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="Cd" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. evansi")+
ggtitle("Te control intraspecific cadmium environment")
save_plot("./NewAnalyses/cxr_normal/pred_Te_INTRA_Control_cadmium.pdf", width=25, height=10)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Tu_Regime==1 & Environment=="N" & Te_Regime==4), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Tu_Regime==1 & Environment=="N" & Te_Regime==4), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu & Te control interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Tu_Regime==1 & Environment=="C" & Te_Regime==4), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Tu_Regime==1 & Environment=="C" & Te_Regime==4), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu & Te control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Te_Regime==4 & Environment=="N" & Tu_Regime==1), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Te_Regime==4 & Environment=="N" & Tu_Regime==1), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="N" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te & Tu control intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Te_Regime==4 & Environment=="C" & Tu_Regime==1), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Te_Regime==4 & Environment=="C" & Tu_Regime==1), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="Cd" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te & Tu control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Tu_Regime==1 & Environment=="N" & Te_Regime==5), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Tu_Regime==1 & Environment=="N" & Te_Regime==5), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu control & Te evolved interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Tu_Regime==1 & Environment=="C" & Te_Regime==5), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Tu_Regime==1 & Environment=="C" & Te_Regime==5), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu control & Te evolved interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Te_Regime==4 & Environment=="N" & Tu_Regime==2), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Te_Regime==4 & Environment=="N" & Tu_Regime==2), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="N" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te control & Tu evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Te_Regime==4 & Environment=="C" & Tu_Regime==2), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Te_Regime==4 & Environment=="C" & Tu_Regime==2), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="Cd" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te control & Tu evolved interspecific cadmium environment")
ca$Replicate<-as.character(ca$Rep)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Tu_Regime==2 & Environment=="N" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Tu_Regime==2 & Environment=="N" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="N" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Tu_Regime==2 & Environment=="C" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Tu_Regime==2 & Environment=="C" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="Cd" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved intraspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Te_Regime==5 & Environment=="N" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Te_Regime==5 & Environment=="N" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="N" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Te_Regime==5 & Environment=="C" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Te_Regime==5 & Environment=="C" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="Cd" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved intraspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Tu_Regime==2 & Environment=="N" & Te_Regime==4), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Tu_Regime==2 & Environment=="N" & Te_Regime==4), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="N" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te control interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Tu_Regime==2 & Environment=="C" & Te_Regime==4), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Tu_Regime==2 & Environment=="C" & Te_Regime==4), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="Cd" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Te_Regime==5 & Environment=="N" & Tu_Regime==1), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Te_Regime==5 & Environment=="N" & Tu_Regime==1), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="N" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu control intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Te_Regime==5 & Environment=="C" & Tu_Regime==1), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Te_Regime==5 & Environment=="C" & Tu_Regime==1), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="Cd" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Tu_Regime==2 & Environment=="N" & Te_Regime==5), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Tu_Regime==2 & Environment=="N" & Te_Regime==5), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="N" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te evolved interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Tu_Regime==2 & Environment=="C" & Te_Regime==5), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Tu_Regime==2 & Environment=="C" & Te_Regime==5), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="Cd" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te evolved interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Te_Regime==5 & Environment=="N" & Tu_Regime==2), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Te_Regime==5 & Environment=="N" & Tu_Regime==2), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="N" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr, Te_Regime==5 & Environment=="C" & Tu_Regime==2), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr, Te_Regime==5 & Environment=="C" & Tu_Regime==2), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="Cd" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu evolved interspecific cadmium environment")
str(param_all_w0)
'data.frame': 36 obs. of 10 variables:
$ Tu_Regime : chr "SR1" "SR2" "SR1" "SR2" ...
$ Te_Regime : chr "SR4" "SR4" "SR5" "SR5" ...
$ Replicate : int 1 1 1 1 2 2 3 3 3 3 ...
$ Environment: chr "N" "N" "N" "N" ...
$ Tu_lambda : num 2.79 2.07 2.79 2.07 2.05 ...
$ Te_lambda : num 4.84 4.84 5.07 5.07 5.12 ...
$ Tu_intra : num 0.06227 -0.00636 0.06227 -0.00636 -0.00608 ...
$ Te_intra : num 0.0213 0.0213 0.0259 0.0259 -0.0236 ...
$ Tu_inter : num 0.0211 0.0165 0.0406 0.0166 -0.0224 ...
$ Te_inter : num 0.0851 0.0548 0.0432 0.1002 0.1675 ...
str(ca)
'data.frame': 3451 obs. of 42 variables:
$ Block : int 1 1 1 1 1 1 1 1 1 1 ...
$ Rep : int 1 1 1 1 1 1 1 1 1 1 ...
$ Box : int 1 1 1 1 1 1 1 1 1 1 ...
$ Leaf : int 3 4 3 4 3 4 3 4 3 4 ...
$ Disk : int 1 2 3 4 5 6 7 8 9 10 ...
$ Env : chr "N" "N" "Cd" "Cd" ...
$ FocalSR : int 4 4 4 4 4 4 4 4 4 4 ...
$ CompSR : int NA NA NA NA NA NA NA NA NA NA ...
$ Dens : int 1 1 1 1 2 2 2 2 4 4 ...
$ Type : chr "INTRA" "INTRA" "INTRA" "INTRA" ...
$ Focalfemale : chr "Te" "Te" "Te" "Te" ...
$ FocalDead : int 0 0 1 0 1 1 0 0 0 0 ...
$ FocalDrowned : int 0 0 0 0 0 0 0 0 0 0 ...
$ FocalMissing : int 0 0 0 0 0 0 0 0 0 0 ...
$ NumbDeadComp : int 0 0 0 0 0 0 0 0 0 0 ...
$ NumberOfEggs : int 11 21 3 9 15 16 17 11 58 24 ...
$ NumberOfEggsBelow : int 0 0 0 0 0 0 0 0 0 0 ...
$ TeMales : int 3 3 0 3 2 5 5 3 8 8 ...
$ TeFemales : int 6 9 2 5 2 10 4 4 9 6 ...
$ TeQuiescentfemales : int 0 0 0 0 0 0 3 0 3 4 ...
$ TuMales : int NA NA NA NA NA NA NA NA NA NA ...
$ TuFemales : int NA NA NA NA NA NA NA NA NA NA ...
$ TuQuiescentfemales : int NA NA NA NA NA NA NA NA NA NA ...
$ Observations : chr NA NA NA NA ...
$ Block2 : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
$ Rep2 : Factor w/ 5 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Disk2 : Factor w/ 16 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
$ Leaf2 : Factor w/ 2 levels "3","4": 1 2 1 2 1 2 1 2 1 2 ...
$ Env2 : Factor w/ 2 levels "Cd","N": 2 2 1 1 2 2 1 1 2 2 ...
$ FocalSR2 : Factor w/ 4 levels "1","2","4","5": 3 3 3 3 3 3 3 3 3 3 ...
$ CompSR2 : Factor w/ 4 levels "1","2","4","5": NA NA NA NA NA NA NA NA NA NA ...
$ Type2 : Factor w/ 2 levels "INTER","INTRA": 2 2 2 2 2 2 2 2 2 2 ...
$ Focal_Female2 : Factor w/ 2 levels "Te","Tu": 1 1 1 1 1 1 1 1 1 1 ...
$ Nr_Focal_Females_Tu_Alive_G0: num NA NA NA NA NA NA NA NA NA NA ...
$ Nr_Focal_Females_Te_Alive_G0: num 1 1 0 1 1 1 2 2 4 4 ...
$ Num_Comp_Tu_Alive_G0 : num NA NA NA NA NA NA NA NA NA NA ...
$ Num_Comp_Te_Alive_G0 : num NA NA NA NA NA NA NA NA NA NA ...
$ Nr_Focal_Females_G0 : num 1 1 1 1 2 2 2 2 4 4 ...
$ Nr_Comp_Females_G0 : num 0 0 0 0 1 1 1 1 3 3 ...
$ GrowthRateOA : num 6 9 2 5 1 5 2 2 2.25 1.5 ...
$ GrowthRateOA_Q : num 6 9 2 5 1 5 3.5 2 3 2.5 ...
$ Replicate : chr "1" "1" "1" "1" ...
rk_func<- function(lambda, alpha_ii, alpha_ij, dens_i, dens_j, ...){
gr<-lambda*exp(-alpha_ii*dens_i - alpha_ij*dens_j)
return(gr)
}
red_ca<-ca[,c("Env", "Rep", "FocalSR", "CompSR", "Dens", "Type", "TeFemales", "TuFemales", "GrowthRateOA")]
red_ca
red_ca$Dens_Focal<-sapply(c(1:length(red_ca[,1])), function(x){
if(red_ca$Type[x]=="INTRA"){
a<-red_ca$Dens[x]-1
}else if(red_ca$Type[x]=="INTER"){
a<-1
}
a
})
red_ca$Dens_Comp<-sapply(c(1:length(red_ca[,1])), function(x){
if(red_ca$Type[x]=="INTRA"){
a<-0
}else if(red_ca$Type[x]=="INTER"){
a<-red_ca$Dens[x]-1
}
a
})
red_ca$Focal<-mapvalues(red_ca$FocalSR, c(1,2,4,5), c("SR1", "SR2","SR4", "SR5"))
red_ca$Comp<-mapvalues(red_ca$CompSR, c(1,2,4,5), c("SR1", "SR2","SR4", "SR5"))
red_ca$pred<-sapply(c(1:length(red_ca[,1])), function(x){
if(red_ca$Focal[x]=="SR1" | red_ca$Focal[x]=="SR2"){
aux_data<-subset(param_all_w0, Environment==red_ca$Env[x] & Replicate== red_ca$Rep[x] & as.character(Tu_Regime)==red_ca$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Tu_lambda[1], alpha_ii =aux_data$Tu_intra[1], alpha_ij = aux_data$Tu_inter[1], dens_i = red_ca$Dens_Focal[x], dens_j = red_ca$Dens_Comp[x])
}else if(red_ca$Focal[x]=="SR4" | red_ca$Focal[x]=="SR5"){
aux_data<-subset(param_all_w0, Environment==red_ca$Env[x] & Replicate== red_ca$Rep[x] & as.character(Te_Regime)==red_ca$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Te_lambda[1], alpha_ii =aux_data$Te_intra[1], alpha_ij = aux_data$Te_inter[1], dens_i = red_ca$Dens_Focal[x], dens_j = red_ca$Dens_Comp[x])
}
aux_pred
})
red_ca$pred_L<-sapply(c(1:length(red_ca[,1])), function(x){
if(red_ca$Focal[x]=="SR1" | red_ca$Focal[x]=="SR2"){
aux_data<-subset(param_all_w0_lower, Environment==red_ca$Env[x] & Replicate== red_ca$Rep[x] & as.character(Tu_Regime)==red_ca$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Tu_lambda[1], alpha_ii =aux_data$Tu_intra[1], alpha_ij = aux_data$Tu_inter[1], dens_i = red_ca$Dens_Focal[x], dens_j = red_ca$Dens_Comp[x])
}else if(red_ca$Focal[x]=="SR4" | red_ca$Focal[x]=="SR5"){
aux_data<-subset(param_all_w0_lower, Environment==red_ca$Env[x] & Replicate== red_ca$Rep[x] & as.character(Te_Regime)==red_ca$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Te_lambda[1], alpha_ii =aux_data$Te_intra[1], alpha_ij = aux_data$Te_inter[1], dens_i = red_ca$Dens_Focal[x], dens_j = red_ca$Dens_Comp[x])
}
aux_pred
})
red_ca$pred_U<-sapply(c(1:length(red_ca[,1])), function(x){
if(red_ca$Focal[x]=="SR1" | red_ca$Focal[x]=="SR2"){
aux_data<-subset(param_all_w0_upper, Environment==red_ca$Env[x] & Replicate== red_ca$Rep[x] & as.character(Tu_Regime)==red_ca$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Tu_lambda[1], alpha_ii =aux_data$Tu_intra[1], alpha_ij = aux_data$Tu_inter[1], dens_i = red_ca$Dens_Focal[x], dens_j = red_ca$Dens_Comp[x])
}else if(red_ca$Focal[x]=="SR4" | red_ca$Focal[x]=="SR5"){
aux_data<-subset(param_all_w0_upper, Environment==red_ca$Env[x] & Replicate== red_ca$Rep[x] & as.character(Te_Regime)==red_ca$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Te_lambda[1], alpha_ii =aux_data$Te_intra[1], alpha_ij = aux_data$Te_inter[1], dens_i = red_ca$Dens_Focal[x], dens_j = red_ca$Dens_Comp[x])
}
aux_pred
})
red_ca$Replicate<-red_ca$Rep
str(red_ca)
'data.frame': 3451 obs. of 17 variables:
$ Env : chr "N" "N" "Cd" "Cd" ...
$ Rep : int 1 1 1 1 1 1 1 1 1 1 ...
$ FocalSR : int 4 4 4 4 4 4 4 4 4 4 ...
$ CompSR : int NA NA NA NA NA NA NA NA NA NA ...
$ Dens : int 1 1 1 1 2 2 2 2 4 4 ...
$ Type : chr "INTRA" "INTRA" "INTRA" "INTRA" ...
$ TeFemales : int 6 9 2 5 2 10 4 4 9 6 ...
$ TuFemales : int NA NA NA NA NA NA NA NA NA NA ...
$ GrowthRateOA: num 6 9 2 5 1 5 2 2 2.25 1.5 ...
$ Dens_Focal : num 0 0 0 0 1 1 1 1 3 3 ...
$ Dens_Comp : num 0 0 0 0 0 0 0 0 0 0 ...
$ Focal : chr "SR4" "SR4" "SR4" "SR4" ...
$ Comp : chr NA NA NA NA ...
$ pred : num 4.84 4.84 2.09 2.09 4.74 ...
$ pred_L : num 3.85 3.85 1.8 1.8 3.92 ...
$ pred_U : num 5.84 5.84 2.38 2.38 5.49 ...
$ Replicate : int 1 1 1 1 1 1 1 1 1 1 ...
ggplot(subset(red_ca, Env=="N" & FocalSR==1), aes(x=log(GrowthRateOA+1), y=log(pred+1)))+
facet_grid(CompSR~Replicate)+
geom_abline(slope = 1, intercept=0)+
geom_point()+
ylab("Predicted")+
xlab("Observed")+
ggtitle("Normal environment Control urticae")
ggplot(subset(red_ca, Env=="N" & FocalSR==1), aes(x=GrowthRateOA, y=pred))+
facet_grid(CompSR~Replicate)+
geom_abline(slope = 1, intercept=0)+
geom_point()+
ylab("Predicted")+
xlab("Observed")+
ggtitle("Normal environment Control urticae")
ggplot(subset(red_ca, Env=="N" & FocalSR==1 & Type=="INTRA"), aes(y=GrowthRateOA, x=Dens_Focal))+
facet_grid(.~Replicate)+
geom_point(position=position_dodge2(0.5))+
geom_line(aes(y=pred, x=Dens_Focal))+
geom_ribbon(aes(ymin=pred_L, ymax=pred_U), fill="lightgrey", linetype="dashed", colour="black")+
ylab("Growth rate")+
xlab("Density")+
ggtitle("Normal environment Control urticae Intra")
ggplot(subset(red_ca, Env=="N" & FocalSR==1 & Type=="INTER" & CompSR=="4"), aes(y=GrowthRateOA, x=Dens_Comp))+
facet_grid(.~Replicate)+
geom_point(position=position_dodge2(0.5))+
geom_abline(data=subset(param_all_w0,Tu_Regime=="SR1" & Environment=="N" & Te_Regime=="SR4"), aes(slope=-Tu_inter, intercept = Tu_lambda))+
ylab("Growth rate")+
xlab("Density")+
ggtitle("Normal environment Control urticae Inter Control")
ggplot(subset(red_ca, Env=="N" & FocalSR==1 & Type=="INTER" & CompSR=="5"), aes(y=GrowthRateOA, x=Dens_Comp))+
facet_grid(.~Replicate)+
geom_point(position=position_dodge2(0.5))+
geom_abline(data=subset(param_all_w0,Tu_Regime=="SR1" & Environment=="N" & Te_Regime=="SR5"), aes(slope=-Tu_inter, intercept = Tu_lambda))+
ylab("Growth rate")+
xlab("Density")+
ggtitle("Normal environment Control urticae Inter Evolved")
NA
NA
cxr accepts a data frame with a first column called fitness with positive values and numeric columns with number of individuals. Each row is one individual. For multiple species the easier is to create a list, each with a data frame that has in the first column number of individuals produced and then the number of neighbours
this case we transformed all 0s into 1 (so that the log is 0) For that we need to add +1 to all data so that the variance is not changed
dir.create("./NewAnalyses/cxr_lambda_fixed_log", showWarnings = FALSE)
# modifying data frame to fit the type of setup that is need for CXR
CXR_B_N<-subset(ca, Env=="N")[,c("Rep", "FocalSR", "CompSR", "Dens", "TeFemales", "TuFemales")]
CXR_B_N$Focal<-mapvalues(CXR_B_N$FocalSR, c(1,2,4,5), c("SR1", "SR2","SR4","SR5"))
CXR_B_N$CompSR2<-mapvalues(CXR_B_N$CompSR, c(1,2,4,5), c("SR1", "SR2","SR4","SR5"))
CXR_B_N$Comp<-sapply(c(1:length(CXR_B_N[,1])), function(x){
if(is.na(CXR_B_N$CompSR2[x])){
a<- CXR_B_N$Focal[x]
}else{
a<-CXR_B_N$CompSR2[x]
}
a
})
aux<-data.frame(SR1=rep(0, length(CXR_B_N[,1])), SR2=rep(0, length(CXR_B_N[,1])), SR4=rep(0, length(CXR_B_N[,1])), SR5=rep(0, length(CXR_B_N[,1])))
for(i in 1:length(CXR_B_N[,1])){
#coluna onde por focais
colunaF<-which(colnames(aux)==CXR_B_N$Focal[i])
#coluna onde por competidors
colunaC<-which(colnames(aux)==CXR_B_N$Comp[i])
#if its the same regime
if(CXR_B_N$Focal[i]==CXR_B_N$Comp[i] & CXR_B_N$Dens[i]==1){
aux[i,colunaF]<-CXR_B_N$Dens[i]-1
}else if(CXR_B_N$Focal[i]==CXR_B_N$Comp[i]){
aux[i,colunaF]<-CXR_B_N$Dens[i]-1
}else{ #if it is heterospecific then its -1 for the competitors (because of the focal) and its one for the focal
aux[i,colunaC]<-CXR_B_N$Dens[i]-1
aux[i, colunaF]<-1
}
}
CXR_B_N<-cbind(CXR_B_N, aux)
CXR_B_N$fitness<-sapply(c(1:length(CXR_B_N[,1])), function(x){
colF<-which(colnames(CXR_B_N)==CXR_B_N$Focal[x])
if(CXR_B_N$Focal[x]=="SR1"){
a<-CXR_B_N$TuFemales[x]/CXR_B_N$SR1[x]
} else if(CXR_B_N$Focal[x]=="SR2"){
a<-CXR_B_N$TuFemales[x]/CXR_B_N$SR2[x]
} else if(CXR_B_N$Focal[x]=="SR4"){
a<-CXR_B_N$TeFemales[x]/CXR_B_N$SR4[x]
} else if(CXR_B_N$Focal[x]=="SR5"){
a<-CXR_B_N$TeFemales[x]/CXR_B_N$SR5[x]
}
a
})
#removing rows for which there is no data for fitness
CXR_B_N<-CXR_B_N[-which(is.na(CXR_B_N$fitness)),]
# adding +1 to all data
#CXR_B_N$fitness<-CXR_B_N$fitness+1
CXR_B_N[which(CXR_B_N$fitness=="-Inf" | CXR_B_N$fitness=="Inf"),"fitness"]<-0
# all data gets +1 because of the 0 problem
CXR_B_N$fitness<-CXR_B_N$fitness+1
# vector that tells which are the selection regimes, the columns have to have the same name
my.reg <- c("SR1", "SR2","SR4","SR5")
# Do list per replicate and environment
R1<-list(SR1= subset(CXR_B_N, Rep==1 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(CXR_B_N, Rep==1 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(CXR_B_N, Rep==1 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(CXR_B_N, Rep==1 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
R2<-list(SR1= subset(CXR_B_N, Rep==2 & Focal=="SR1")[,c("fitness", "SR1", "SR4", "SR5")], SR4= subset(CXR_B_N, Rep==2 & Focal=="SR4")[,c("fitness", "SR1", "SR4", "SR5")], SR5= subset(CXR_B_N, Rep==2 & Focal=="SR5")[,c("fitness", "SR1", "SR4", "SR5")])
R3<-list(SR1= subset(CXR_B_N, Rep==3 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(CXR_B_N, Rep==3 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(CXR_B_N, Rep==3 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(CXR_B_N, Rep==3 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
R4<-list(SR1= subset(CXR_B_N, Rep==4 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(CXR_B_N, Rep==4 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(CXR_B_N, Rep==4 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(CXR_B_N, Rep==4 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
R5<-list(SR1= subset(CXR_B_N, Rep==5 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(CXR_B_N, Rep==5 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(CXR_B_N, Rep==5 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(CXR_B_N, Rep==5 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
mean_dens1<-data.frame(SR=c(rep(1,10), rep(2,8), rep(4,10),rep(5,10)), Env=c(rep("N", 5),rep("Cd", 5), rep("N", 4),rep("Cd", 4),rep("N", 5),rep("Cd", 5),rep("N", 5),rep("Cd", 5)), Rep=c(rep(c(1,2,3,4,5),2),rep(c(1,3,4,5),2),rep(c(1,2,3,4,5),2),rep(c(1,2,3,4,5),2)))
#since in the model we use the log of data +1, here we also have to use the +1 to estimate the lambda
mean_dens1$lambda<-sapply(c(1:length(mean_dens1[,1])), function(x){
mean(subset(ca, FocalSR==mean_dens1$SR[x] & Dens==1 & Env==mean_dens1$Env[x] & Rep==mean_dens1$Rep[x] )$GrowthRateOA+1, na.rm=TRUE)
})
mean_dens1$sd_lambda<-sapply(c(1:length(mean_dens1[,1])), function(x){
sd(subset(ca, FocalSR==mean_dens1$SR[x] & Dens==1 & Env==mean_dens1$Env[x] & Rep==mean_dens1$Rep[x])$GrowthRateOA+1, na.rm=TRUE)
})
mean_dens1$sd_lambda[which(is.na(mean_dens1$sd_lambda))]<-0.01
mean_dens1$sd_lambda[which(mean_dens1$sd_lambda==0)]<-0.01
#### lambda
fixed_terms_1N <- list(list(lambda = subset(mean_dens1, Rep==1 & Env=="N" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==1 & Env=="N" & SR==2)$lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==1 & Env=="N" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==1 & Env=="N" & SR==5)$lambda))
fixed_terms_2N <- list(list(lambda = subset(mean_dens1, Rep==2 & Env=="N" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==2 & Env=="N" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==2 & Env=="N" & SR==5)$lambda))
fixed_terms_3N <- list(list(lambda = subset(mean_dens1, Rep==3 & Env=="N" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==3 & Env=="N" & SR==2)$lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==3 & Env=="N" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==3 & Env=="N" & SR==5)$lambda))
fixed_terms_4N <- list(list(lambda = subset(mean_dens1, Rep==4 & Env=="N" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==4 & Env=="N" & SR==2)$lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==4 & Env=="N" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==4 & Env=="N" & SR==5)$lambda))
fixed_terms_5N <- list(list(lambda = subset(mean_dens1, Rep==5 & Env=="N" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==5 & Env=="N" & SR==2)$lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==5 & Env=="N" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==5 & Env=="N" & SR==5)$lambda))
cxr_B.R1_w0<-cxr_pm_multifit(data = R1,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = fixed_terms_1N,
# no standard errors
bootstrap_samples = 200)
cxr_B.R2_w0<-cxr_pm_multifit(data = R2,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = fixed_terms_2N,
# no standard errors
bootstrap_samples = 200)
cxr_pm_fit: a focal column is provided, but it
contains no densities > 0. It will be discarded.
Error in all_neigh_matrix[, z] : subscript out of bounds
cxr_pm_bootstrap ERROR : Cannot evaluate function at initial parameters
cxr_pm_fit: a focal column is provided, but it
contains no densities > 0. It will be discarded.
Error in all_neigh_matrix[, z] : subscript out of bounds
cxr_pm_bootstrap ERROR : Cannot evaluate function at initial parameters
cxr_B.R3_w0<-cxr_pm_multifit(data = R3,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = fixed_terms_3N,
# no standard errors
bootstrap_samples = 200)
cxr_B.R4_w0<-cxr_pm_multifit(data = R4,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = fixed_terms_3N,
# no standard errors
bootstrap_samples = 200)
cxr_B.R5_w0<-cxr_pm_multifit(data = R5,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = fixed_terms_5N,
# no standard errors
bootstrap_samples = 200)
summary(cxr_B.R1_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4 SR5
SR1 0.1152073 NA 0.06228645 0.08110864
SR2 NA 0.08740496 0.08543745 0.08550777
SR4 0.1856830 0.15249776 0.15904629 NA
SR5 0.1298725 0.18237359 NA 0.13676259
summary(cxr_B.R2_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR4 SR5
SR1 0.1172894 0.06509954 0.01802708
SR4 0.1470504 0.07173698 NA
SR5 0.1505072 NA 0.15769431
summary(cxr_B.R3_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4 SR5
SR1 0.1049483 NA 0.08653311 0.07231917
SR2 NA 0.04410714 0.03756058 0.01975269
SR4 0.1502023 0.08148835 0.11380584 NA
SR5 0.1140247 0.09888509 NA 0.18246871
summary(cxr_B.R4_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4 SR5
SR1 0.1159686 NA 0.1158970 0.13195567
SR2 NA 0.1287665 0.0969120 0.06779875
SR4 0.1733748 0.1552535 0.2017891 NA
SR5 0.1756829 0.1620642 NA 0.18269408
summary(cxr_B.R5_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4
SR1 0.02834927 NA 0.06040919
SR2 NA 0.05122945 0.07768747
SR4 0.14021797 0.16510937 0.13032500
SR5 0.19789959 0.16403343 NA
SR5
SR1 0.03234430
SR2 -0.02517362
SR4 NA
SR5 0.14473888
#ab<-abundance_projection(cxr_B.R1_w0, timesteps = 1, initial_abundances = c(3,3,3,3))
rows in the alpha element of the returning list correspond to species i and columns to species j for each αij coefficient.
cxr_param_B<-expand.grid(Tu_Regime=c("SR1", "SR2"), Te_Regime=c("SR4", "SR5"), Replicate=c(1,2,3,4,5), Environment=c("N"))
cxr_param_B$Tu_lambda<-0
cxr_param_B$Te_lambda<-0
cxr_param_B$Tu_intra<-0
cxr_param_B$Te_intra<-0
cxr_param_B$Tu_inter<-0
cxr_param_B$Te_inter<-0
#removing SR2 for replicate 2
cxr_param_B<-cxr_param_B[-which(cxr_param_B$Replicate==2 & cxr_param_B$Tu_Regime=="SR2"),]
cxr_param_B[which(cxr_param_B$Replicate==1),"Tu_lambda"]<-c(cxr_B.R1_w0$fixed_terms[[1]]$lambda,cxr_B.R1_w0$fixed_terms[[2]]$lambda)
cxr_param_B[which(cxr_param_B$Replicate==1),"Te_lambda"]<-c(cxr_B.R1_w0$fixed_terms[[3]]$lambda,cxr_B.R1_w0$fixed_terms[[3]]$lambda, cxr_B.R1_w0$fixed_terms[[4]]$lambda,cxr_B.R1_w0$fixed_terms[[4]]$lambda)
cxr_param_B[which(cxr_param_B$Replicate==2),"Tu_lambda"]<-c(cxr_B.R2_w0$fixed_terms[[1]]$lambda,cxr_B.R2_w0$fixed_terms[[1]]$lambda)
cxr_param_B[which(cxr_param_B$Replicate==2),"Te_lambda"]<-c(cxr_B.R2_w0$fixed_terms[[2]]$lambda,cxr_B.R2_w0$fixed_terms[[3]]$lambda)
cxr_param_B[which(cxr_param_B$Replicate==3),"Tu_lambda"]<-c(cxr_B.R3_w0$fixed_terms[[1]]$lambda,cxr_B.R3_w0$fixed_terms[[2]]$lambda)
cxr_param_B[which(cxr_param_B$Replicate==3),"Te_lambda"]<-c(cxr_B.R3_w0$fixed_terms[[3]]$lambda,cxr_B.R3_w0$fixed_terms[[3]]$lambda, cxr_B.R3_w0$fixed_terms[[4]]$lambda,cxr_B.R3_w0$fixed_terms[[4]]$lambda)
cxr_param_B[which(cxr_param_B$Replicate==4),"Tu_lambda"]<-c(cxr_B.R4_w0$fixed_terms[[1]]$lambda,cxr_B.R4_w0$fixed_terms[[2]]$lambda)
cxr_param_B[which(cxr_param_B$Replicate==4),"Te_lambda"]<-c(cxr_B.R4_w0$fixed_terms[[3]]$lambda,cxr_B.R4_w0$fixed_terms[[3]]$lambda, cxr_B.R4_w0$fixed_terms[[4]]$lambda,cxr_B.R4_w0$fixed_terms[[4]]$lambda)
cxr_param_B[which(cxr_param_B$Replicate==5),"Tu_lambda"]<-c(cxr_B.R5_w0$fixed_terms[[1]]$lambda,cxr_B.R5_w0$fixed_terms[[2]]$lambda)
cxr_param_B[which(cxr_param_B$Replicate==5),"Te_lambda"]<-c(cxr_B.R5_w0$fixed_terms[[3]]$lambda,cxr_B.R5_w0$fixed_terms[[3]]$lambda, cxr_B.R5_w0$fixed_terms[[4]]$lambda,cxr_B.R5_w0$fixed_terms[[4]]$lambda)
cxr_param_B[which(cxr_param_B$Replicate==1),"Tu_intra"]<-rep(c(cxr_B.R1_w0$alpha_matrix[1,1], cxr_B.R1_w0$alpha_matrix[2,2]), 2)
cxr_param_B[which(cxr_param_B$Replicate==1),"Te_intra"]<-rep(c(cxr_B.R1_w0$alpha_matrix[3,3], cxr_B.R1_w0$alpha_matrix[4,4]), each=2)
cxr_param_B[which(cxr_param_B$Replicate==2),"Tu_intra"]<-rep(c(cxr_B.R2_w0$alpha_matrix[1,1]), 2)
cxr_param_B[which(cxr_param_B$Replicate==2),"Te_intra"]<-rep(c(cxr_B.R2_w0$alpha_matrix[2,2], cxr_B.R2_w0$alpha_matrix[3,3]))
cxr_param_B[which(cxr_param_B$Replicate==3),"Tu_intra"]<-rep(c(cxr_B.R3_w0$alpha_matrix[1,1], cxr_B.R3_w0$alpha_matrix[2,2]), 2)
cxr_param_B[which(cxr_param_B$Replicate==3),"Te_intra"]<-rep(c(cxr_B.R3_w0$alpha_matrix[3,3], cxr_B.R3_w0$alpha_matrix[4,4]), each=2)
cxr_param_B[which(cxr_param_B$Replicate==4),"Tu_intra"]<-rep(c(cxr_B.R4_w0$alpha_matrix[1,1], cxr_B.R4_w0$alpha_matrix[2,2]), 2)
cxr_param_B[which(cxr_param_B$Replicate==4),"Te_intra"]<-rep(c(cxr_B.R4_w0$alpha_matrix[3,3], cxr_B.R4_w0$alpha_matrix[4,4]), each=2)
cxr_param_B[which(cxr_param_B$Replicate==5),"Tu_intra"]<-rep(c(cxr_B.R5_w0$alpha_matrix[1,1], cxr_B.R5_w0$alpha_matrix[2,2]), 2)
cxr_param_B[which(cxr_param_B$Replicate==5),"Te_intra"]<-rep(c(cxr_B.R5_w0$alpha_matrix[3,3], cxr_B.R5_w0$alpha_matrix[4,4]), each=2)
cxr_param_B[which(cxr_param_B$Replicate==1),"Tu_inter"]<-c(cxr_B.R1_w0$alpha_matrix[1,3], cxr_B.R1_w0$alpha_matrix[2,3],cxr_B.R1_w0$alpha_matrix[1,4], cxr_B.R1_w0$alpha_matrix[2,4])
cxr_param_B[which(cxr_param_B$Replicate==1),"Te_inter"]<-c(cxr_B.R1_w0$alpha_matrix[3,1], cxr_B.R1_w0$alpha_matrix[3,2],cxr_B.R1_w0$alpha_matrix[4,1], cxr_B.R1_w0$alpha_matrix[4,2])
cxr_param_B[which(cxr_param_B$Replicate==2),"Tu_inter"]<-c(cxr_B.R2_w0$alpha_matrix[1,2], cxr_B.R2_w0$alpha_matrix[1,3])
cxr_param_B[which(cxr_param_B$Replicate==2),"Te_inter"]<-c(cxr_B.R2_w0$alpha_matrix[2,1],cxr_B.R2_w0$alpha_matrix[3,1])
cxr_param_B[which(cxr_param_B$Replicate==3),"Tu_inter"]<-c(cxr_B.R3_w0$alpha_matrix[1,3], cxr_B.R3_w0$alpha_matrix[2,3],cxr_B.R3_w0$alpha_matrix[1,4], cxr_B.R3_w0$alpha_matrix[2,4])
cxr_param_B[which(cxr_param_B$Replicate==3),"Te_inter"]<-c(cxr_B.R3_w0$alpha_matrix[3,1], cxr_B.R3_w0$alpha_matrix[3,2],cxr_B.R3_w0$alpha_matrix[4,1], cxr_B.R3_w0$alpha_matrix[4,2])
cxr_param_B[which(cxr_param_B$Replicate==4),"Tu_inter"]<-c(cxr_B.R4_w0$alpha_matrix[1,3], cxr_B.R4_w0$alpha_matrix[2,3],cxr_B.R4_w0$alpha_matrix[1,4], cxr_B.R4_w0$alpha_matrix[2,4])
cxr_param_B[which(cxr_param_B$Replicate==4),"Te_inter"]<-c(cxr_B.R4_w0$alpha_matrix[3,1], cxr_B.R4_w0$alpha_matrix[3,2],cxr_B.R4_w0$alpha_matrix[4,1], cxr_B.R4_w0$alpha_matrix[4,2])
cxr_param_B[which(cxr_param_B$Replicate==5),"Tu_inter"]<-c(cxr_B.R5_w0$alpha_matrix[1,3], cxr_B.R5_w0$alpha_matrix[2,3],cxr_B.R5_w0$alpha_matrix[1,4], cxr_B.R5_w0$alpha_matrix[2,4])
cxr_param_B[which(cxr_param_B$Replicate==5),"Te_inter"]<-c(cxr_B.R5_w0$alpha_matrix[3,1], cxr_B.R5_w0$alpha_matrix[3,2],cxr_B.R5_w0$alpha_matrix[4,1], cxr_B.R5_w0$alpha_matrix[4,2])
### Lower
cxr_param_B_lower<-expand.grid(Tu_Regime=c("SR1", "SR2"), Te_Regime=c("SR4", "SR5"), Replicate=c(1,2,3,4,5), Environment=c("N"))
cxr_param_B_lower$Tu_lambda<-0
cxr_param_B_lower$Te_lambda<-0
cxr_param_B_lower$Tu_intra<-0
cxr_param_B_lower$Te_intra<-0
cxr_param_B_lower$Tu_inter<-0
cxr_param_B_lower$Te_inter<-0
#removing SR2 for replicate 2
cxr_param_B_lower<-cxr_param_B_lower[-which(cxr_param_B_lower$Replicate==2 & cxr_param_B_lower$Tu_Regime=="SR2"),]
#Since the error comes directly from the data we need to create some lists with that information
sd_1N <- list(list(lambda = subset(mean_dens1, Rep==1 & Env=="N" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==1 & Env=="N" & SR==2)$sd_lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==1 & Env=="N" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==1 & Env=="N" & SR==5)$sd_lambda))
sd_2N <- list(list(lambda = subset(mean_dens1, Rep==2 & Env=="N" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==2 & Env=="N" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==2 & Env=="N" & SR==5)$sd_lambda))
sd_3N <- list(list(lambda = subset(mean_dens1, Rep==3 & Env=="N" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==3 & Env=="N" & SR==2)$sd_lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==3 & Env=="N" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==3 & Env=="N" & SR==5)$sd_lambda))
sd_4N <- list(list(lambda = subset(mean_dens1, Rep==4 & Env=="N" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==4 & Env=="N" & SR==2)$sd_lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==4 & Env=="N" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==4 & Env=="N" & SR==5)$sd_lambda))
sd_5N <- list(list(lambda = subset(mean_dens1, Rep==5 & Env=="N" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==5 & Env=="N" & SR==2)$sd_lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==5 & Env=="N" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==5 & Env=="N" & SR==5)$sd_lambda))
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==1),"Tu_lambda"]<-c(cxr_B.R1_w0$fixed_terms[[1]]$lambda-sd_1N[[1]]$lambda,cxr_B.R1_w0$fixed_terms[[2]]$lambda-sd_1N[[2]]$lambda)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==1),"Te_lambda"]<-c(cxr_B.R1_w0$fixed_terms[[3]]$lambda-sd_1N[[3]]$lambda,cxr_B.R1_w0$fixed_terms[[3]]$lambda-sd_1N[[3]]$lambda, cxr_B.R1_w0$fixed_terms[[4]]$lambda-sd_1N[[4]]$lambda,cxr_B.R1_w0$fixed_terms[[4]]$lambda-sd_1N[[4]]$lambda)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==2),"Tu_lambda"]<-c(cxr_B.R2_w0$fixed_terms[[1]]$lambda-sd_2N[[1]]$lambda,cxr_B.R2_w0$fixed_terms[[1]]$lambda-sd_2N[[1]]$lambda)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==2),"Te_lambda"]<-c(cxr_B.R2_w0$fixed_terms[[2]]$lambda-sd_2N[[2]]$lambda,cxr_B.R2_w0$fixed_terms[[3]]$lambda-sd_2N[[3]]$lambda)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==3),"Tu_lambda"]<-c(cxr_B.R3_w0$fixed_terms[[1]]$lambda-sd_3N[[1]]$lambda,cxr_B.R3_w0$fixed_terms[[2]]$lambda-sd_3N[[2]]$lambda)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==3),"Te_lambda"]<-c(cxr_B.R3_w0$fixed_terms[[3]]$lambda-sd_3N[[3]]$lambda,cxr_B.R3_w0$fixed_terms[[3]]$lambda-sd_3N[[3]]$lambda, cxr_B.R3_w0$fixed_terms[[4]]$lambda-sd_3N[[4]]$lambda,cxr_B.R3_w0$fixed_terms[[4]]$lambda-sd_3N[[4]]$lambda)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==4),"Tu_lambda"]<-c(cxr_B.R4_w0$fixed_terms[[1]]$lambda-sd_4N[[1]]$lambda,cxr_B.R4_w0$fixed_terms[[2]]$lambda-sd_4N[[2]]$lambda)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==4),"Te_lambda"]<-c(cxr_B.R4_w0$fixed_terms[[3]]$lambda-sd_4N[[3]]$lambda,cxr_B.R4_w0$fixed_terms[[3]]$lambda-sd_4N[[3]]$lambda, cxr_B.R4_w0$fixed_terms[[4]]$lambda-sd_4N[[4]]$lambda,cxr_B.R4_w0$fixed_terms[[4]]$lambda-sd_4N[[4]]$lambda)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==5),"Tu_lambda"]<-c(cxr_B.R5_w0$fixed_terms[[1]]$lambda-sd_5N[[1]]$lambda,cxr_B.R5_w0$fixed_terms[[2]]$lambda-sd_5N[[2]]$lambda)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==5),"Te_lambda"]<-c(cxr_B.R5_w0$fixed_terms[[3]]$lambda-sd_5N[[3]]$lambda,cxr_B.R5_w0$fixed_terms[[3]]$lambda-sd_5N[[3]]$lambda, cxr_B.R5_w0$fixed_terms[[4]]$lambda-sd_5N[[4]]$lambda,cxr_B.R5_w0$fixed_terms[[4]]$lambda-sd_5N[[4]]$lambda)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==1),"Tu_intra"]<-rep(c(cxr_B.R1_w0$alpha_matrix[1,1]-cxr_B.R1_w0$alpha_matrix_standard_error[1,1], cxr_B.R1_w0$alpha_matrix[2,2]-cxr_B.R1_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==1),"Te_intra"]<-rep(c(cxr_B.R1_w0$alpha_matrix[3,3]-cxr_B.R1_w0$alpha_matrix_standard_error[3,3], cxr_B.R1_w0$alpha_matrix[4,4]-cxr_B.R1_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==2),"Tu_intra"]<-rep(c(cxr_B.R2_w0$alpha_matrix[1,1]-cxr_B.R2_w0$alpha_matrix_standard_error[1,1]), 2)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==2),"Te_intra"]<-c(cxr_B.R2_w0$alpha_matrix[2,2]-cxr_B.R2_w0$alpha_matrix_standard_error[2,2], cxr_B.R2_w0$alpha_matrix[3,3]-cxr_B.R2_w0$alpha_matrix_standard_error[3,3])
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==3),"Tu_intra"]<-rep(c(cxr_B.R3_w0$alpha_matrix[1,1]-cxr_B.R3_w0$alpha_matrix_standard_error[1,1], cxr_B.R3_w0$alpha_matrix[2,2]-cxr_B.R3_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==3),"Te_intra"]<-rep(c(cxr_B.R3_w0$alpha_matrix[3,3]-cxr_B.R3_w0$alpha_matrix_standard_error[3,3], cxr_B.R3_w0$alpha_matrix[4,4]-cxr_B.R3_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==4),"Tu_intra"]<-rep(c(cxr_B.R4_w0$alpha_matrix[1,1]-cxr_B.R4_w0$alpha_matrix_standard_error[1,1], cxr_B.R4_w0$alpha_matrix[2,2]-cxr_B.R4_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==4),"Te_intra"]<-rep(c(cxr_B.R4_w0$alpha_matrix[3,3]-cxr_B.R4_w0$alpha_matrix_standard_error[3,3], cxr_B.R4_w0$alpha_matrix[4,4]-cxr_B.R4_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==5),"Tu_intra"]<-rep(c(cxr_B.R5_w0$alpha_matrix[1,1]-cxr_B.R5_w0$alpha_matrix_standard_error[1,1], cxr_B.R5_w0$alpha_matrix[2,2]-cxr_B.R5_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==5),"Te_intra"]<-rep(c(cxr_B.R5_w0$alpha_matrix[3,3]-cxr_B.R5_w0$alpha_matrix_standard_error[3,3], cxr_B.R5_w0$alpha_matrix[4,4]-cxr_B.R5_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==1),"Tu_inter"]<-c(cxr_B.R1_w0$alpha_matrix[1,3]-cxr_B.R1_w0$alpha_matrix_standard_error[1,3], cxr_B.R1_w0$alpha_matrix[2,3]-cxr_B.R1_w0$alpha_matrix_standard_error[2,3],cxr_B.R1_w0$alpha_matrix[1,4]-cxr_B.R1_w0$alpha_matrix_standard_error[1,4], cxr_B.R1_w0$alpha_matrix[2,4]-cxr_B.R1_w0$alpha_matrix_standard_error[2,4])
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==1),"Te_inter"]<-c(cxr_B.R1_w0$alpha_matrix[3,1]-cxr_B.R1_w0$alpha_matrix_standard_error[3,1], cxr_B.R1_w0$alpha_matrix[3,2]-cxr_B.R1_w0$alpha_matrix_standard_error[3,2],cxr_B.R1_w0$alpha_matrix[4,1]-cxr_B.R1_w0$alpha_matrix_standard_error[4,1], cxr_B.R1_w0$alpha_matrix[4,2]-cxr_B.R1_w0$alpha_matrix_standard_error[4,2])
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==2),"Tu_inter"]<-c(cxr_B.R2_w0$alpha_matrix[1,2]-cxr_B.R2_w0$alpha_matrix_standard_error[1,2], cxr_B.R2_w0$alpha_matrix[1,3]-cxr_B.R2_w0$alpha_matrix_standard_error[1,3])
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==2),"Te_inter"]<-c(cxr_B.R2_w0$alpha_matrix[2,1]-cxr_B.R2_w0$alpha_matrix_standard_error[2,1], cxr_B.R2_w0$alpha_matrix[3,1]-cxr_B.R2_w0$alpha_matrix_standard_error[3,1])
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==3),"Tu_inter"]<-c(cxr_B.R3_w0$alpha_matrix[1,3]-cxr_B.R3_w0$alpha_matrix_standard_error[1,3], cxr_B.R3_w0$alpha_matrix[2,3]-cxr_B.R3_w0$alpha_matrix_standard_error[2,3],cxr_B.R3_w0$alpha_matrix[1,4]-cxr_B.R3_w0$alpha_matrix_standard_error[1,4], cxr_B.R3_w0$alpha_matrix[2,4]-cxr_B.R3_w0$alpha_matrix_standard_error[2,4])
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==3),"Te_inter"]<-c(cxr_B.R3_w0$alpha_matrix[3,1]-cxr_B.R3_w0$alpha_matrix_standard_error[3,1], cxr_B.R3_w0$alpha_matrix[3,2]-cxr_B.R3_w0$alpha_matrix_standard_error[3,2],cxr_B.R3_w0$alpha_matrix[4,1]-cxr_B.R3_w0$alpha_matrix_standard_error[4,1], cxr_B.R3_w0$alpha_matrix[4,2]-cxr_B.R3_w0$alpha_matrix_standard_error[4,2])
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==4),"Tu_inter"]<-c(cxr_B.R4_w0$alpha_matrix[1,3]-cxr_B.R4_w0$alpha_matrix_standard_error[1,3], cxr_B.R4_w0$alpha_matrix[2,3]-cxr_B.R4_w0$alpha_matrix_standard_error[2,3],cxr_B.R4_w0$alpha_matrix[1,4]-cxr_B.R4_w0$alpha_matrix_standard_error[1,4], cxr_B.R4_w0$alpha_matrix[2,4]-cxr_B.R4_w0$alpha_matrix_standard_error[2,4])
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==4),"Te_inter"]<-c(cxr_B.R4_w0$alpha_matrix[3,1]-cxr_B.R4_w0$alpha_matrix_standard_error[3,1], cxr_B.R4_w0$alpha_matrix[3,2]-cxr_B.R4_w0$alpha_matrix_standard_error[3,2],cxr_B.R4_w0$alpha_matrix[4,1]-cxr_B.R4_w0$alpha_matrix_standard_error[4,1], cxr_B.R4_w0$alpha_matrix[4,2]-cxr_B.R4_w0$alpha_matrix_standard_error[4,2])
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==5),"Tu_inter"]<-c(cxr_B.R5_w0$alpha_matrix[1,3]-cxr_B.R5_w0$alpha_matrix_standard_error[1,3], cxr_B.R5_w0$alpha_matrix[2,3]-cxr_B.R5_w0$alpha_matrix_standard_error[2,3],cxr_B.R5_w0$alpha_matrix[1,4]-cxr_B.R5_w0$alpha_matrix_standard_error[1,4], cxr_B.R5_w0$alpha_matrix[2,4]-cxr_B.R5_w0$alpha_matrix_standard_error[2,4])
cxr_param_B_lower[which(cxr_param_B_lower$Replicate==5),"Te_inter"]<-c(cxr_B.R5_w0$alpha_matrix[3,1]-cxr_B.R5_w0$alpha_matrix_standard_error[3,1], cxr_B.R5_w0$alpha_matrix[3,2]-cxr_B.R5_w0$alpha_matrix_standard_error[3,2],cxr_B.R5_w0$alpha_matrix[4,1]-cxr_B.R5_w0$alpha_matrix_standard_error[4,1], cxr_B.R5_w0$alpha_matrix[4,2]-cxr_B.R5_w0$alpha_matrix_standard_error[4,2])
### upper
cxr_param_B_upper<-expand.grid(Tu_Regime=c("SR1", "SR2"), Te_Regime=c("SR4", "SR5"), Replicate=c(1,2,3,4,5), Environment=c("N"))
cxr_param_B_upper$Tu_lambda<-0
cxr_param_B_upper$Te_lambda<-0
cxr_param_B_upper$Tu_intra<-0
cxr_param_B_upper$Te_intra<-0
cxr_param_B_upper$Tu_inter<-0
cxr_param_B_upper$Te_inter<-0
#removing SR2 for replicate 2
cxr_param_B_upper<-cxr_param_B_upper[-which(cxr_param_B_upper$Replicate==2 & cxr_param_B_upper$Tu_Regime=="SR2"),]
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==1),"Tu_lambda"]<-c(cxr_B.R1_w0$fixed_terms[[1]]$lambda+sd_1N[[1]]$lambda,cxr_B.R1_w0$fixed_terms[[2]]$lambda+sd_1N[[2]]$lambda)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==1),"Te_lambda"]<-c(cxr_B.R1_w0$fixed_terms[[3]]$lambda+sd_1N[[3]]$lambda,cxr_B.R1_w0$fixed_terms[[3]]$lambda+sd_1N[[3]]$lambda, cxr_B.R1_w0$fixed_terms[[4]]$lambda+sd_1N[[4]]$lambda,cxr_B.R1_w0$fixed_terms[[4]]$lambda+sd_1N[[4]]$lambda)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==2),"Tu_lambda"]<-c(cxr_B.R2_w0$fixed_terms[[1]]$lambda+sd_2N[[1]]$lambda,cxr_B.R2_w0$fixed_terms[[1]]$lambda+sd_2N[[1]]$lambda)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==2),"Te_lambda"]<-c(cxr_B.R2_w0$fixed_terms[[2]]$lambda+sd_2N[[2]]$lambda,cxr_B.R2_w0$fixed_terms[[3]]$lambda+sd_2N[[3]]$lambda)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==3),"Tu_lambda"]<-c(cxr_B.R3_w0$fixed_terms[[1]]$lambda+sd_3N[[1]]$lambda,cxr_B.R3_w0$fixed_terms[[2]]$lambda+sd_3N[[2]]$lambda)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==3),"Te_lambda"]<-c(cxr_B.R3_w0$fixed_terms[[3]]$lambda+sd_3N[[3]]$lambda,cxr_B.R3_w0$fixed_terms[[3]]$lambda+sd_3N[[3]]$lambda, cxr_B.R3_w0$fixed_terms[[4]]$lambda+sd_3N[[4]]$lambda,cxr_B.R3_w0$fixed_terms[[4]]$lambda+sd_3N[[4]]$lambda)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==4),"Tu_lambda"]<-c(cxr_B.R4_w0$fixed_terms[[1]]$lambda+sd_4N[[1]]$lambda,cxr_B.R4_w0$fixed_terms[[2]]$lambda+sd_4N[[2]]$lambda)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==4),"Te_lambda"]<-c(cxr_B.R4_w0$fixed_terms[[3]]$lambda+sd_4N[[3]]$lambda,cxr_B.R4_w0$fixed_terms[[3]]$lambda+sd_4N[[3]]$lambda, cxr_B.R4_w0$fixed_terms[[4]]$lambda+sd_4N[[4]]$lambda,cxr_B.R4_w0$fixed_terms[[4]]$lambda+sd_4N[[4]]$lambda)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==5),"Tu_lambda"]<-c(cxr_B.R5_w0$fixed_terms[[1]]$lambda+sd_5N[[1]]$lambda,cxr_B.R5_w0$fixed_terms[[2]]$lambda+sd_5N[[2]]$lambda)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==5),"Te_lambda"]<-c(cxr_B.R5_w0$fixed_terms[[3]]$lambda+sd_5N[[3]]$lambda,cxr_B.R5_w0$fixed_terms[[3]]$lambda+sd_5N[[3]]$lambda, cxr_B.R5_w0$fixed_terms[[4]]$lambda+sd_5N[[4]]$lambda,cxr_B.R5_w0$fixed_terms[[4]]$lambda+sd_5N[[4]]$lambda)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==1),"Tu_intra"]<-rep(c(cxr_B.R1_w0$alpha_matrix[1,1]+cxr_B.R1_w0$alpha_matrix_standard_error[1,1], cxr_B.R1_w0$alpha_matrix[2,2]+cxr_B.R1_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==1),"Te_intra"]<-rep(c(cxr_B.R1_w0$alpha_matrix[3,3]+cxr_B.R1_w0$alpha_matrix_standard_error[3,3], cxr_B.R1_w0$alpha_matrix[4,4]+cxr_B.R1_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==2),"Tu_intra"]<-rep(c(cxr_B.R2_w0$alpha_matrix[1,1]+cxr_B.R2_w0$alpha_matrix_standard_error[1,1]), 2)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==2),"Te_intra"]<-c(cxr_B.R2_w0$alpha_matrix[2,2]+cxr_B.R2_w0$alpha_matrix_standard_error[2,2], cxr_B.R2_w0$alpha_matrix[3,3]+cxr_B.R2_w0$alpha_matrix_standard_error[3,3])
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==3),"Tu_intra"]<-rep(c(cxr_B.R3_w0$alpha_matrix[1,1]+cxr_B.R3_w0$alpha_matrix_standard_error[1,1], cxr_B.R3_w0$alpha_matrix[2,2]+cxr_B.R3_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==3),"Te_intra"]<-rep(c(cxr_B.R3_w0$alpha_matrix[3,3]+cxr_B.R3_w0$alpha_matrix_standard_error[3,3], cxr_B.R3_w0$alpha_matrix[4,4]+cxr_B.R3_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==4),"Tu_intra"]<-rep(c(cxr_B.R4_w0$alpha_matrix[1,1]+cxr_B.R4_w0$alpha_matrix_standard_error[1,1], cxr_B.R4_w0$alpha_matrix[2,2]+cxr_B.R4_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==4),"Te_intra"]<-rep(c(cxr_B.R4_w0$alpha_matrix[3,3]+cxr_B.R4_w0$alpha_matrix_standard_error[3,3], cxr_B.R4_w0$alpha_matrix[4,4]+cxr_B.R4_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==5),"Tu_intra"]<-rep(c(cxr_B.R5_w0$alpha_matrix[1,1]+cxr_B.R5_w0$alpha_matrix_standard_error[1,1], cxr_B.R5_w0$alpha_matrix[2,2]+cxr_B.R5_w0$alpha_matrix_standard_error[2,2]), 2)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==5),"Te_intra"]<-rep(c(cxr_B.R5_w0$alpha_matrix[3,3]+cxr_B.R5_w0$alpha_matrix_standard_error[3,3], cxr_B.R5_w0$alpha_matrix[4,4]+cxr_B.R5_w0$alpha_matrix_standard_error[4,4]), each=2)
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==1),"Tu_inter"]<-c(cxr_B.R1_w0$alpha_matrix[1,3]+cxr_B.R1_w0$alpha_matrix_standard_error[1,3], cxr_B.R1_w0$alpha_matrix[2,3]+cxr_B.R1_w0$alpha_matrix_standard_error[2,3],cxr_B.R1_w0$alpha_matrix[1,4]+cxr_B.R1_w0$alpha_matrix_standard_error[1,4], cxr_B.R1_w0$alpha_matrix[2,4]+cxr_B.R1_w0$alpha_matrix_standard_error[2,4])
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==1),"Te_inter"]<-c(cxr_B.R1_w0$alpha_matrix[3,1]+cxr_B.R1_w0$alpha_matrix_standard_error[3,1], cxr_B.R1_w0$alpha_matrix[3,2]+cxr_B.R1_w0$alpha_matrix_standard_error[3,2],cxr_B.R1_w0$alpha_matrix[4,1]+cxr_B.R1_w0$alpha_matrix_standard_error[4,1], cxr_B.R1_w0$alpha_matrix[4,2]+cxr_B.R1_w0$alpha_matrix_standard_error[4,2])
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==2),"Tu_inter"]<-c(cxr_B.R2_w0$alpha_matrix[1,2]+cxr_B.R2_w0$alpha_matrix_standard_error[1,2], cxr_B.R2_w0$alpha_matrix[1,3]+cxr_B.R2_w0$alpha_matrix_standard_error[1,3])
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==2),"Te_inter"]<-c(cxr_B.R2_w0$alpha_matrix[2,1]+cxr_B.R2_w0$alpha_matrix_standard_error[2,1], cxr_B.R2_w0$alpha_matrix[3,1]+cxr_B.R2_w0$alpha_matrix_standard_error[3,1])
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==3),"Tu_inter"]<-c(cxr_B.R3_w0$alpha_matrix[1,3]+cxr_B.R3_w0$alpha_matrix_standard_error[1,3], cxr_B.R3_w0$alpha_matrix[2,3]+cxr_B.R3_w0$alpha_matrix_standard_error[2,3],cxr_B.R3_w0$alpha_matrix[1,4]+cxr_B.R3_w0$alpha_matrix_standard_error[1,4], cxr_B.R3_w0$alpha_matrix[2,4]+cxr_B.R3_w0$alpha_matrix_standard_error[2,4])
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==3),"Te_inter"]<-c(cxr_B.R3_w0$alpha_matrix[3,1]+cxr_B.R3_w0$alpha_matrix_standard_error[3,1], cxr_B.R3_w0$alpha_matrix[3,2]+cxr_B.R3_w0$alpha_matrix_standard_error[3,2],cxr_B.R3_w0$alpha_matrix[4,1]+cxr_B.R3_w0$alpha_matrix_standard_error[4,1], cxr_B.R3_w0$alpha_matrix[4,2]+cxr_B.R3_w0$alpha_matrix_standard_error[4,2])
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==4),"Tu_inter"]<-c(cxr_B.R4_w0$alpha_matrix[1,3]+cxr_B.R4_w0$alpha_matrix_standard_error[1,3], cxr_B.R4_w0$alpha_matrix[2,3]+cxr_B.R4_w0$alpha_matrix_standard_error[2,3],cxr_B.R4_w0$alpha_matrix[1,4]+cxr_B.R4_w0$alpha_matrix_standard_error[1,4], cxr_B.R4_w0$alpha_matrix[2,4]+cxr_B.R4_w0$alpha_matrix_standard_error[2,4])
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==4),"Te_inter"]<-c(cxr_B.R4_w0$alpha_matrix[3,1]+cxr_B.R4_w0$alpha_matrix_standard_error[3,1], cxr_B.R4_w0$alpha_matrix[3,2]+cxr_B.R4_w0$alpha_matrix_standard_error[3,2],cxr_B.R4_w0$alpha_matrix[4,1]+cxr_B.R4_w0$alpha_matrix_standard_error[4,1], cxr_B.R4_w0$alpha_matrix[4,2]+cxr_B.R4_w0$alpha_matrix_standard_error[4,2])
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==5),"Tu_inter"]<-c(cxr_B.R5_w0$alpha_matrix[1,3]+cxr_B.R5_w0$alpha_matrix_standard_error[1,3], cxr_B.R5_w0$alpha_matrix[2,3]+cxr_B.R5_w0$alpha_matrix_standard_error[2,3],cxr_B.R5_w0$alpha_matrix[1,4]+cxr_B.R5_w0$alpha_matrix_standard_error[1,4], cxr_B.R5_w0$alpha_matrix[2,4]+cxr_B.R5_w0$alpha_matrix_standard_error[2,4])
cxr_param_B_upper[which(cxr_param_B_upper$Replicate==5),"Te_inter"]<-c(cxr_B.R5_w0$alpha_matrix[3,1]+cxr_B.R5_w0$alpha_matrix_standard_error[3,1], cxr_B.R5_w0$alpha_matrix[3,2]+cxr_B.R5_w0$alpha_matrix_standard_error[3,2],cxr_B.R5_w0$alpha_matrix[4,1]+cxr_B.R5_w0$alpha_matrix_standard_error[4,1], cxr_B.R5_w0$alpha_matrix[4,2]+cxr_B.R5_w0$alpha_matrix_standard_error[4,2])
# modifying data frame to fit the type of setup that is need for CXR
CXR_B_Cd<-subset(ca, Env=="Cd")[,c("Rep", "FocalSR", "CompSR", "Dens", "TeFemales", "TuFemales")]
CXR_B_Cd$Focal<-mapvalues(CXR_B_Cd$FocalSR, c(1,2,4,5), c("SR1", "SR2","SR4","SR5"))
CXR_B_Cd$CompSR2<-mapvalues(CXR_B_Cd$CompSR, c(1,2,4,5), c("SR1", "SR2","SR4","SR5"))
CXR_B_Cd$Comp<-sapply(c(1:length(CXR_B_Cd[,1])), function(x){
if(is.na(CXR_B_Cd$CompSR2[x])){
a<- CXR_B_Cd$Focal[x]
}else{
a<-CXR_B_Cd$CompSR2[x]
}
a
})
aux<-data.frame(SR1=rep(0, length(CXR_B_Cd[,1])), SR2=rep(0, length(CXR_B_Cd[,1])), SR4=rep(0, length(CXR_B_Cd[,1])), SR5=rep(0, length(CXR_B_Cd[,1])))
for(i in 1:length(CXR_B_Cd[,1])){
#coluna onde por focais
colunaF<-which(colnames(aux)==CXR_B_Cd$Focal[i])
#coluna onde por competidors
colunaC<-which(colnames(aux)==CXR_B_Cd$Comp[i])
#if its the same regime
if(CXR_B_Cd$Focal[i]==CXR_B_Cd$Comp[i] & CXR_B_Cd$Dens[i]==1){
aux[i,colunaF]<-CXR_B_Cd$Dens[i]-1
}else if(CXR_B_Cd$Focal[i]==CXR_B_Cd$Comp[i]){
aux[i,colunaF]<-CXR_B_Cd$Dens[i]-1
}else{ #if it is heterospecific then its -1 for the competitors (because of the focal) and its one for the focal
aux[i,colunaC]<-CXR_B_Cd$Dens[i]-1
aux[i, colunaF]<-1
}
}
CXR_B_Cd<-cbind(CXR_B_Cd, aux)
CXR_B_Cd$fitness<-sapply(c(1:length(CXR_B_Cd[,1])), function(x){
colF<-which(colnames(CXR_B_Cd)==CXR_B_Cd$Focal[x])
if(CXR_B_Cd$Focal[x]=="SR1"){
a<-CXR_B_Cd$TuFemales[x]/CXR_B_Cd$SR1[x]
} else if(CXR_B_Cd$Focal[x]=="SR2"){
a<-CXR_B_Cd$TuFemales[x]/CXR_B_Cd$SR2[x]
} else if(CXR_B_Cd$Focal[x]=="SR4"){
a<-CXR_B_Cd$TeFemales[x]/CXR_B_Cd$SR4[x]
} else if(CXR_B_Cd$Focal[x]=="SR5"){
a<-CXR_B_Cd$TeFemales[x]/CXR_B_Cd$SR5[x]
}
a
})
#removing rows for which there is no data for fitness
#CXR_B_Cd<-CXR_B_Cd[-which(is.na(CXR_B_Cd$fitness)),]
#CXR_B_Cd$fitness<-CXR_B_Cd$fitness+1
CXR_B_Cd[which(CXR_B_Cd$fitness=="-Inf" | CXR_B_Cd$fitness=="Inf"),"fitness"]<-0
#0 to 1 to mainrain data
CXR_B_Cd<-CXR_B_Cd[-which(is.na(CXR_B_Cd$fitness)),]
CXR_B_Cd$fitness<-CXR_B_Cd$fitness+1
# vector that tells which are the selection regimes, the columns have to have the same name
my.reg <- c("SR1", "SR2","SR4","SR5")
# Do list per replicate and environment
R1_Cd<-list(SR1= subset(CXR_B_Cd, Rep==1 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(CXR_B_Cd, Rep==1 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(CXR_B_Cd, Rep==1 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(CXR_B_Cd, Rep==1 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
R2_Cd<-list(SR1= subset(CXR_B_Cd, Rep==2 & Focal=="SR1")[,c("fitness", "SR1", "SR2","SR4", "SR5")], SR4= subset(CXR_B_Cd, Rep==2 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(CXR_B_Cd, Rep==2 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
R3_Cd<-list(SR1= subset(CXR_B_Cd, Rep==3 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(CXR_B_Cd, Rep==3 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(CXR_B_Cd, Rep==3 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(CXR_B_Cd, Rep==3 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
R4_Cd<-list(SR1= subset(CXR_B_Cd, Rep==4 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(CXR_B_Cd, Rep==4 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(CXR_B_Cd, Rep==4 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(CXR_B_Cd, Rep==4 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
R5_Cd<-list(SR1= subset(CXR_B_Cd, Rep==5 & Focal=="SR1")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR2= subset(CXR_B_Cd, Rep==5 & Focal=="SR2")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR4= subset(CXR_B_Cd, Rep==5 & Focal=="SR4")[,c("fitness", "SR1", "SR2", "SR4", "SR5")], SR5= subset(CXR_B_Cd, Rep==5 & Focal=="SR5")[,c("fitness", "SR1", "SR2", "SR4", "SR5")])
fixed_terms_1Cd <- list(list(lambda = subset(mean_dens1, Rep==1 & Env=="Cd" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==1 & Env=="Cd" & SR==2)$lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==1 & Env=="Cd" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==1 & Env=="Cd" & SR==5)$lambda))
fixed_terms_2Cd <- list(list(lambda = subset(mean_dens1, Rep==2 & Env=="Cd" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==2 & Env=="Cd" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==2 & Env=="Cd" & SR==5)$lambda))
fixed_terms_3Cd <- list(list(lambda = subset(mean_dens1, Rep==3 & Env=="Cd" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==3 & Env=="Cd" & SR==2)$lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==3 & Env=="Cd" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==3 & Env=="Cd" & SR==5)$lambda))
fixed_terms_4Cd <- list(list(lambda = subset(mean_dens1, Rep==4 & Env=="Cd" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==4 & Env=="Cd" & SR==2)$lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==4 & Env=="Cd" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==4 & Env=="Cd" & SR==5)$lambda))
fixed_terms_5Cd <- list(list(lambda = subset(mean_dens1, Rep==5 & Env=="Cd" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==5 & Env=="Cd" & SR==2)$lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==5 & Env=="Cd" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==5 & Env=="Cd" & SR==5)$lambda))
cxr_B.R1_Cd_w0<-cxr_pm_multifit(data = R1_Cd,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = fixed_terms_1Cd,
# no standard errors
bootstrap_samples = 200)
# replicate 2 below
cxr_B.R3_Cd_w0<-cxr_pm_multifit(data = R3_Cd,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = fixed_terms_3Cd,
# no standard errors
bootstrap_samples =10)
cxr_B.R4_Cd_w0<-cxr_pm_multifit(data = R4_Cd,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = fixed_terms_4Cd,
# no standard errors
bootstrap_samples = 200)
cxr_B.R5_Cd_w0<-cxr_pm_multifit(data = R5_Cd,
focal_column = my.reg,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = fixed_terms_5Cd,
# no standard errors
bootstrap_samples = 200)
summary(cxr_B.R1_Cd_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4 SR5
SR1 0.04584548 NA 0.05556988 0.04349890
SR2 NA 0.06757583 0.04081173 0.04680983
SR4 0.03890394 0.07072264 0.09908637 NA
SR5 0.11799967 0.09104722 NA 0.15488950
#summary(cxr_B.R2_Cd_w0)
summary(cxr_B.R3_Cd_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4
SR1 0.002343429 NA -0.012755707
SR2 NA -0.023284929 -0.006172213
SR4 0.083002640 0.007698104 0.056114178
SR5 0.068766280 0.022130484 NA
SR5
SR1 -0.01294586
SR2 -0.01497267
SR4 NA
SR5 0.06106163
summary(cxr_B.R4_Cd_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4
SR1 0.05359984 NA 0.037647969
SR2 NA 0.014609303 0.005563145
SR4 0.02483767 0.094226200 0.099437652
SR5 0.06218425 0.001456832 NA
SR5
SR1 0.04685735
SR2 -0.01089155
SR4 NA
SR5 0.04178655
summary(cxr_B.R5_Cd_w0)
model: 'RK_pm_alpha_pairwise_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
SR1 SR2 SR4
SR1 0.025879570 NA 0.03814373
SR2 NA 0.05776287 0.06843702
SR4 0.009265975 0.03998117 0.06246490
SR5 -0.008943842 -0.01236246 NA
SR5
SR1 -0.02413730
SR2 0.05740409
SR4 NA
SR5 0.03308326
### For replicate I can't find good initial conditions, lets do this by hand
# cxr_B.R2_Cd_w0<-cxr_pm_multifit(data = R2_Cd,
# focal_column = my.reg[c(1,3,4)],
# model_family = "BH",
# covariates = NULL,
# optimization_method = "Nelder-Mead",
# alpha_form = "pairwise",
# lambda_cov_form = "none",
# alpha_cov_form = "none",
# initial_values = list(lambda = 1,
# alpha_intra = 0.1,
# alpha_inter = 0.1),
# fixed_terms = NULL,
# # no standard errors
# bootstrap_samples =10)
# This one works well
cxr_B.R2_Cd_w0_sr1<-cxr_pm_fit(data = R2_Cd[[1]],
focal_column = my.reg[1],
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_intra = 0.1,
alpha_inter = 0.1),
fixed_terms = fixed_terms_2Cd[[1]],
# no standard errors
bootstrap_samples = 200)
#for replicate 2 we will do the fitting by hand because we may need to scale the parameters
cxr_B.R2_Cd_w0_sr4<-cxr_pm_fit(data = R2_Cd[[2]][which(R2_Cd[[2]][,"SR1"]==0), c("fitness", "SR4")],
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_2Cd[[2]],
# no standard errors
bootstrap_samples = 200)
cxr_B.R2_Cd_w0_5<-cxr_pm_fit(data = R2_Cd[[3]][which(R2_Cd[[3]][,"SR1"]==0), c("fitness", "SR5")],
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_2Cd[[3]],
# no standard errors
bootstrap_samples = 200)
cxr_B.R2_Cd_w0_sr4_inter<-cxr_pm_fit(data = R2_Cd[[2]][which(R2_Cd[[2]][,"SR1"]!=0), c("fitness", "SR1")],
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_2Cd[[2]],
# no standard errors
bootstrap_samples = 200)
cxr_B.R2_Cd_w0_sr5_inter<-cxr_pm_fit(data = R2_Cd[[3]][which(R2_Cd[[3]][,"SR1"]!=0), c("fitness", "SR1")],
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_2Cd[[3]],
# no standard errors
bootstrap_samples = 200)
cxr_param_BC_upper[which(cxr_param_BC_upper$Replicate==2),"Te_intra"]<-c(cxr_B.R2_Cd_w0_sr4$alpha_inter[1]+cxr_B.R2_Cd_w0_sr4$alpha_inter_standard_error[1], cxr_B.R2_Cd_w0_sr5$alpha_inter[1]+cxr_B.R2_Cd_w0_sr5$alpha_inter_standard_error[1])
Error: object 'cxr_B.R2_Cd_w0_sr5' not found
param_all_B<-as.data.frame(rbind(cxr_param_B, cxr_param_BC))
param_all_B_lower<-as.data.frame(rbind(cxr_param_B_lower, cxr_param_BC_lower))
param_all_B_upper<-as.data.frame(rbind(cxr_param_B_upper, cxr_param_BC_upper))
param_all_B_lower
param_all_B_upper
write.csv(param_all_B, "./NewAnalyses/cxr_lambda_fixed_log/parameters_cxr_lambda_fixed.csv")
write.csv(param_all_B_upper, "./NewAnalyses/cxr_lambda_fixed_log/parameters_cxr_lambda_fixed_upper.csv")
write.csv(param_all_B_lower, "./NewAnalyses/cxr_lambda_fixed_log/parameters_cxr_lambda_fixed_lower.csv")
param_all_B<-read.csv("./NewAnalyses/cxr_lambda_fixed_log/parameters_cxr_lambda_fixed.csv")
param_all_B_upper<-read.csv("./NewAnalyses/cxr_lambda_fixed_log/parameters_cxr_lambda_fixed_upper.csv")
param_all_B_lower<-read.csv("./NewAnalyses/cxr_lambda_fixed_log/parameters_cxr_lambda_fixed_lower.csv")
param_all_B<-param_all_B[,-1]
param_all_B_upper<-param_all_B_upper[,-1]
param_all_B_lower<-param_all_B_lower[,-1]
param_all_B_long<-gather(param_all_B, parameter, value,Tu_lambda:Te_inter )
param_all_B_long$category<-mapvalues(param_all_B_long$parameter, c("Tu_lambda", "Te_lambda", "Tu_intra", "Te_intra","Tu_inter", "Te_inter"), c("lambda", "lambda", "intra", "intra", "inter", "inter"))
param_all_B_lower_long<-gather(param_all_B_lower, parameter, value,Tu_lambda:Te_inter )
param_all_B_lower_long$category<-mapvalues(param_all_B_lower_long$parameter, c("Tu_lambda", "Te_lambda", "Tu_intra", "Te_intra","Tu_inter", "Te_inter"), c("lambda", "lambda", "intra", "intra", "inter", "inter"))
param_all_B_upper_long<-gather(param_all_B_upper, parameter, value,Tu_lambda:Te_inter )
param_all_B_upper_long$category<-mapvalues(param_all_B_upper_long$parameter, c("Tu_lambda", "Te_lambda", "Tu_intra", "Te_intra","Tu_inter", "Te_inter"), c("lambda", "lambda", "intra", "intra", "inter", "inter"))
colnames(param_all_B_lower_long)[6]<-"lower"
colnames(param_all_B_upper_long)[6]<-"upper"
str(param_all_B_long)
'data.frame': 216 obs. of 7 variables:
$ Tu_Regime : chr "SR1" "SR2" "SR1" "SR2" ...
$ Te_Regime : chr "SR4" "SR4" "SR5" "SR5" ...
$ Replicate : int 1 1 1 1 2 2 3 3 3 3 ...
$ Environment: chr "N" "N" "N" "N" ...
$ parameter : chr "Tu_lambda" "Tu_lambda" "Tu_lambda" "Tu_lambda" ...
$ value : num 3.9 3.7 3.9 3.7 4.2 ...
$ category : chr "lambda" "lambda" "lambda" "lambda" ...
param_all_B_long<-cbind(param_all_B_long[,1:7],param_all_B_lower_long$lower, param_all_B_upper_long$upper)
colnames(param_all_B_long)[8:9]<-c("lower","upper")
ggplot(subset(param_all_B_long, parameter=="Te_intra" & Tu_Regime=="SR1"), aes(x=Te_Regime, y=value, fill=Environment))+
geom_boxplot( position=position_dodge2(0.5), outlier.alpha = 99)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("Evolved \nwithout cadmium", "Evolved \nwith cadmium"))+
scale_fill_manual(values=c("#D55E00", "#009E73"), labels=c("Water", "Cadmium"))+
scale_colour_manual(values=c("#D55E00", "#009E73"), labels=c("Water", "Cadmium"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Selection regime")
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_intraTe_w0_rep.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Tu_intra" & Te_Regime=="SR4"), aes(x=Tu_Regime, y=value, fill=Environment))+
geom_boxplot(position=position_dodge2(0.5), outlier.alpha = 99)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("Evolved \nwithout cadmium", "Evolved \nwith cadmium"))+
scale_fill_manual(values=c("#D55E00", "#009E73"), labels=c("Water", "Cadmium"))+
scale_colour_manual(values=c("#D55E00", "#009E73"), labels=c("Water", "Cadmium"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Selection regime")
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_intraTu_w0_rep.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Te_intra" & Tu_Regime=="SR1" & Environment=="Cd"), aes(x=Te_Regime, y=value, fill=Te_Regime))+
geom_boxplot(position=position_dodge2(0.5), outlier.alpha = 99)+
geom_point(alpha=0.85, position=position_dodge2(0.5))+
theme_ines+
scale_fill_manual(values=c("#D55E00", "#009E73"))+
scale_x_discrete(labels=c("Evolved \nwithout cadmium", "Evolved \nwith cadmium"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Selection regime")
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_intraTe_w0_rep_evol.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Tu_intra" & Te_Regime=="SR4" & Environment=="Cd"), aes(x=Tu_Regime, y=value, fill=Tu_Regime))+
geom_boxplot( position=position_dodge2(0.5), outlier.alpha = 99)+
geom_point(alpha=0.85, position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("Evolved \nwithout cadmium", "Evolved \nwith cadmium"))+
scale_fill_manual(values=c("#D55E00", "#009E73"))+
scale_colour_manual(values=c("#D55E00", "#009E73"), labels=c("Water", "Cadmium"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Selection regime")
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_intraTu_w0_rep_evol.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Te_intra" & Tu_Regime=="SR1"), aes(x=Environment, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Homogeneous"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Homogeneous"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_intraTe_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Tu_intra" & Te_Regime=="SR4"), aes(fill=Tu_Regime, y=value, x=Environment))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Homogeneous"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Homogeneous"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_intraTu_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Te_inter"), aes(x=Environment, y=value, fill=Te_Regime))+
facet_grid(Tu_Regime~., labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Homogeneous"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Homogeneous"))+
ylab("Interspecific competition (T. evansi)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_interTe_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Tu_inter"), aes(fill=Tu_Regime, y=value, x=Environment))+
facet_grid(Te_Regime~., labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot(outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.75))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Homogeneous"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Homogeneous"))+
ylab("Interspecific competition (T. urticae)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_interTu_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Te_lambda" & Tu_Regime=="SR1"), aes(x=Environment, y=value, fill=Te_Regime))+
geom_boxplot( position=position_dodge2(0.5), outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Homogeneous"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Homogeneous"))+
ylab("Intrinsic growth rate (T. evansi)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_lambdaTe_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Tu_lambda" & Te_Regime=="SR4"), aes(fill=Tu_Regime, y=value, x=Environment))+
geom_boxplot(position=position_dodge2(0.5), outlier.alpha =0.01)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Homogeneous"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Homogeneous"))+
ylab("Intrinsic growth rate (T. urticae)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_lambdaTu_w0_rep_env.pdf", width=20, height=15)
###########
ggplot(subset(param_all_B_long, parameter=="Te_intra" & Tu_Regime=="SR1" & Environment=="Cd"), aes(x=Environment, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Environment")+
ylim(c(0.03,0.27))
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_intraTe_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Te_intra" & Tu_Regime=="SR1" & Environment=="N"), aes(x=Environment, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Environment")+
ylim(c(0.03,0.27))
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_intraTe_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Tu_intra" & Te_Regime=="SR4" & Environment=="Cd"), aes(fill=Tu_Regime, y=value, x=Environment))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Environment")+
ylim(c(0.03,0.27))
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_intraTu_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Tu_intra" & Te_Regime=="SR4" & Environment=="N"), aes(fill=Tu_Regime, y=value, x=Environment))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Environment")+
ylim(c(0.03,0.27))
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_intraTu_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Te_inter" & Environment=="Cd"), aes(x=Environment, y=value, fill=Te_Regime))+
facet_grid(.~Tu_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. evansi)")+
xlab("Environment")+
ylim(c(-0.05, 0.16))
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_interTe_w0_rep_Cd.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Te_inter" & Environment=="N"), aes(x=Environment, y=value, fill=Te_Regime))+
facet_grid(.~Tu_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. evansi)")+
xlab("Environment")+
ylim(c(-0.05, 0.16))
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_interTe_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Tu_inter" & Environment=="Cd"), aes(fill=Tu_Regime, y=value, x=Environment))+
facet_grid(.~Te_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot(outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.75))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. urticae)")+
xlab("Environment")+
ylim(c(-0.05, 0.12))
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_interTu_w0_rep_Cd.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Tu_inter" & Environment=="N"), aes(fill=Tu_Regime, y=value, x=Environment))+
facet_grid(.~Te_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot(outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.75))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. urticae)")+
xlab("Environment")+
ylim(c(-0.05, 0.12))
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_interTu_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Te_lambda" & Tu_Regime=="SR1" & Environment=="Cd"), aes(x=Environment, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. evansi)")+
xlab("Environment")+
ylim(c(0, 3))
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_lambdaTe_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Te_lambda" & Tu_Regime=="SR1" & Environment=="N"), aes(x=Environment, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. evansi)")+
xlab("Environment")+
ylim(c(0, 8))
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_lambdaTe_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Tu_lambda" & Te_Regime=="SR4" & Environment=="Cd"), aes(fill=Tu_Regime, y=value, x=Environment))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c( "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. urticae)")+
xlab("Environment")+
ylim(c(0, 3))
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_lambdaTu_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(param_all_B_long, parameter=="Tu_lambda" & Te_Regime=="SR4" & Environment=="N"), aes(fill=Tu_Regime, y=value, x=Environment))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. urticae)")+
xlab("Environment")+
ylim(c(0, 8))
save_plot("./NewAnalyses/cxr_lambda_fixed_log/param_RK_lambdaTu_w0_rep_N.pdf", width=20, height=15)
NA
NA
density_aux<-seq(0, 10, by=(10/100))
pred_df_cxr_B<-as.data.frame(expand_grid(Density=density_aux, Tu_Regime=c("SR1","SR2"), Te_Regime=c("SR4","SR5"), Replicate=c(1:5), Environment=c("N", "Cd")))
pred_df_cxr_B$Tu_mean_intra<-sapply(c(1:length(pred_df_cxr_B[,1])), function(x){
alpha_i<-subset(param_all_B, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_B, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_B, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr_B$Density[x])
pred
})
pred_df_cxr_B$Tu_mean_inter<-sapply(c(1:length(pred_df_cxr_B[,1])), function(x){
alpha_i<-subset(param_all_B, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_B, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_B, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr_B$Density[x])
pred
})
pred_df_cxr_B$Tu_intra_L<-sapply(c(1:length(pred_df_cxr_B[,1])), function(x){
alpha_i<-subset(param_all_B_lower, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_B_lower, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_B_lower, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr_B$Density[x])
pred
})
pred_df_cxr_B$Tu_inter_L<-sapply(c(1:length(pred_df_cxr_B[,1])), function(x){
alpha_i<-subset(param_all_B_lower, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_B_lower, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_B_lower, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr_B$Density[x])
pred
})
pred_df_cxr_B$Tu_intra_U<-sapply(c(1:length(pred_df_cxr_B[,1])), function(x){
alpha_i<-subset(param_all_B_upper, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_B_upper, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_B_upper, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr_B$Density[x])
pred
})
pred_df_cxr_B$Tu_inter_U<-sapply(c(1:length(pred_df_cxr_B[,1])), function(x){
alpha_i<-subset(param_all_B_upper, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_B_upper, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_B_upper, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr_B$Density[x])
pred
})
pred_df_cxr_B$Te_mean_intra<-sapply(c(1:length(pred_df_cxr_B[,1])), function(x){
alpha_i<-subset(param_all_B, Environment==pred_df_cxr_B$Environment[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_B, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_B, Environment==pred_df_cxr_B$Environment[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr_B$Density[x])
pred
})
pred_df_cxr_B$Te_mean_inter<-sapply(c(1:length(pred_df_cxr_B[,1])), function(x){
alpha_i<-subset(param_all_B, Environment==pred_df_cxr_B$Environment[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_B, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_B, Environment==pred_df_cxr_B$Environment[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr_B$Density[x])
pred
})
pred_df_cxr_B$Te_intra_L<-sapply(c(1:length(pred_df_cxr_B[,1])), function(x){
alpha_i<-subset(param_all_B_lower, Environment==pred_df_cxr_B$Environment[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_B_lower, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_B_lower, Environment==pred_df_cxr_B$Environment[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr_B$Density[x])
pred
})
pred_df_cxr_B$Te_inter_L<-sapply(c(1:length(pred_df_cxr_B[,1])), function(x){
alpha_i<-subset(param_all_B_lower, Environment==pred_df_cxr_B$Environment[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_B_lower, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_B_lower, Environment==pred_df_cxr_B$Environment[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr_B$Density[x])
pred
})
pred_df_cxr_B$Te_intra_U<-sapply(c(1:length(pred_df_cxr_B[,1])), function(x){
alpha_i<-subset(param_all_B_upper, Environment==pred_df_cxr_B$Environment[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_B_upper, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_B_upper, Environment==pred_df_cxr_B$Environment[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr_B$Density[x])
pred
})
pred_df_cxr_B$Te_inter_U<-sapply(c(1:length(pred_df_cxr_B[,1])), function(x){
alpha_i<-subset(param_all_B_upper, Environment==pred_df_cxr_B$Environment[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_B_upper, Environment==pred_df_cxr_B$Environment[x] & Tu_Regime==pred_df_cxr_B$Tu_Regime[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_B_upper, Environment==pred_df_cxr_B$Environment[x] & Te_Regime==pred_df_cxr_B$Te_Regime[x] & Replicate==pred_df_cxr_B$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr_B$Density[x])
pred
})
# Removing Tu evolved replicate 2 because there is no data
pred_df_cxr_B<-pred_df_cxr_B[-which(pred_df_cxr_B$Tu_Regime=="SR2" & pred_df_cxr_B$Replicate==2),]
# Transforming everything bellow 0 into 0 for the lower interval
pred_df_cxr_B$Te_inter_L[which(pred_df_cxr_B$Te_inter_L<0)]<-0
pred_df_cxr_B$Te_intra_L[which(pred_df_cxr_B$Te_intra_L<0)]<-0
pred_df_cxr_B$Tu_inter_L[which(pred_df_cxr_B$Tu_inter_L<0)]<-0
pred_df_cxr_B$Tu_intra_L[which(pred_df_cxr_B$Tu_intra_L<0)]<-0
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="N" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="N" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="Cd" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="Cd" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific cadmium environment")
ca$Replicate<-as.character(ca$Rep)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="N" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="N" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific control environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_log/pred_Tu_INTRA_Control_water.pdf", width=25, height=10)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="Cd" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="Cd" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific cadmium environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_log/pred_Tu_INTRA_Control_cadmium.pdf", width=25, height=10)
#subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="Cd" & Replicate==3)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Te_Regime=="SR4" & Environment=="N" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Te_Regime=="SR4" & Environment=="N" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="N" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. evansi")+
ggtitle("Te control intraspecific control environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_log/pred_Te_INTRA_Control_control.pdf", width=25, height=10)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Te_Regime=="SR4" & Environment=="Cd" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Te_Regime=="SR4" & Environment=="Cd" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="Cd" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. evansi")+
ggtitle("Te control intraspecific cadmium environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_log/pred_Te_INTRA_Control_cadmium.pdf", width=25, height=10)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="N" & Te_Regime=="SR4"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="N" & Te_Regime=="SR4"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu & Te control interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="Cd" & Te_Regime=="SR4"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="Cd" & Te_Regime=="SR4"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu & Te control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Te_Regime=="SR4" & Environment=="N" & Tu_Regime=="SR1"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Te_Regime=="SR4" & Environment=="N" & Tu_Regime=="SR1"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="N" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te & Tu control intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Te_Regime=="SR4" & Environment=="Cd" & Tu_Regime=="SR1"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Te_Regime=="SR4" & Environment=="Cd" & Tu_Regime=="SR1"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="Cd" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te & Tu control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="N" & Te_Regime=="SR5"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="N" & Te_Regime=="SR5"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu control & Te evolved interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="Cd" & Te_Regime=="SR5"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Tu_Regime=="SR1" & Environment=="Cd" & Te_Regime=="SR5"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu control & Te evolved interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Te_Regime=="SR4" & Environment=="N" & Tu_Regime=="SR2"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Te_Regime=="SR4" & Environment=="N" & Tu_Regime=="SR2"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="N" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te control & Tu evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Te_Regime=="SR4" & Environment=="Cd" & Tu_Regime=="SR2"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Te_Regime=="SR4" & Environment=="Cd" & Tu_Regime=="SR2"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="Cd" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te control & Tu evolved interspecific cadmium environment")
ca$Replicate<-as.character(ca$Rep)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Tu_Regime=="SR2" & Environment=="N" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Tu_Regime=="SR2" & Environment=="N" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="N" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Tu_Regime=="SR2" & Environment=="Cd" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Tu_Regime=="SR2" & Environment=="Cd" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="Cd" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved intraspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Te_Regime=="SR5" & Environment=="N" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Te_Regime=="SR5" & Environment=="N" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="N" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Te_Regime=="SR5" & Environment=="Cd" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Te_Regime=="SR5" & Environment=="Cd" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="Cd" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved intraspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Tu_Regime=="SR2" & Environment=="N" & Te_Regime=="SR4"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Tu_Regime=="SR2" & Environment=="N" & Te_Regime=="SR4"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="N" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te control interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Tu_Regime=="SR2" & Environment=="Cd" & Te_Regime=="SR4"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Tu_Regime=="SR2" & Environment=="Cd" & Te_Regime=="SR4"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="Cd" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Te_Regime=="SR5" & Environment=="N" & Tu_Regime=="SR1"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Te_Regime=="SR5" & Environment=="N" & Tu_Regime=="SR1"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="N" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu control intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Te_Regime=="SR5" & Environment=="Cd" & Tu_Regime=="SR1"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Te_Regime=="SR5" & Environment=="Cd" & Tu_Regime=="SR1"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="Cd" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Tu_Regime=="SR2" & Environment=="N" & Te_Regime=="SR5"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Tu_Regime=="SR2" & Environment=="N" & Te_Regime=="SR5"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="N" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te evolved interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Tu_Regime=="SR2" & Environment=="Cd" & Te_Regime=="SR5"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Tu_Regime=="SR2" & Environment=="Cd" & Te_Regime=="SR5"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="Cd" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te evolved interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Te_Regime=="SR5" & Environment=="N" & Tu_Regime=="SR2"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Te_Regime=="SR5" & Environment=="N" & Tu_Regime=="SR2"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="N" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_B, Te_Regime=="SR5" & Environment=="Cd" & Tu_Regime=="SR2"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_B, Te_Regime=="SR5" & Environment=="Cd" & Tu_Regime=="SR2"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="Cd" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu evolved interspecific cadmium environment")
str(param_all_B)
'data.frame': 36 obs. of 10 variables:
$ Tu_Regime : chr "SR1" "SR2" "SR1" "SR2" ...
$ Te_Regime : chr "SR4" "SR4" "SR5" "SR5" ...
$ Replicate : int 1 1 1 1 2 2 3 3 3 3 ...
$ Environment: chr "N" "N" "N" "N" ...
$ Tu_lambda : num 3.9 3.7 3.9 3.7 4.2 ...
$ Te_lambda : num 11.11 11.11 10 10 9.86 ...
$ Tu_intra : num 0.1152 0.0874 0.1152 0.0874 0.1173 ...
$ Te_intra : num 0.159 0.159 0.1368 0.1368 0.0717 ...
$ Tu_inter : num 0.0623 0.0854 0.0811 0.0855 0.0651 ...
$ Te_inter : num 0.186 0.152 0.13 0.182 0.147 ...
str(ca)
'data.frame': 3451 obs. of 42 variables:
$ Block : int 1 1 1 1 1 1 1 1 1 1 ...
$ Rep : int 1 1 1 1 1 1 1 1 1 1 ...
$ Box : int 1 1 1 1 1 1 1 1 1 1 ...
$ Leaf : int 3 4 3 4 3 4 3 4 3 4 ...
$ Disk : int 1 2 3 4 5 6 7 8 9 10 ...
$ Env : chr "N" "N" "Cd" "Cd" ...
$ FocalSR : int 4 4 4 4 4 4 4 4 4 4 ...
$ CompSR : int NA NA NA NA NA NA NA NA NA NA ...
$ Dens : int 1 1 1 1 2 2 2 2 4 4 ...
$ Type : chr "INTRA" "INTRA" "INTRA" "INTRA" ...
$ Focalfemale : chr "Te" "Te" "Te" "Te" ...
$ FocalDead : int 0 0 1 0 1 1 0 0 0 0 ...
$ FocalDrowned : int 0 0 0 0 0 0 0 0 0 0 ...
$ FocalMissing : int 0 0 0 0 0 0 0 0 0 0 ...
$ NumbDeadComp : int 0 0 0 0 0 0 0 0 0 0 ...
$ NumberOfEggs : int 11 21 3 9 15 16 17 11 58 24 ...
$ NumberOfEggsBelow : int 0 0 0 0 0 0 0 0 0 0 ...
$ TeMales : int 3 3 0 3 2 5 5 3 8 8 ...
$ TeFemales : int 6 9 2 5 2 10 4 4 9 6 ...
$ TeQuiescentfemales : int 0 0 0 0 0 0 3 0 3 4 ...
$ TuMales : int NA NA NA NA NA NA NA NA NA NA ...
$ TuFemales : int NA NA NA NA NA NA NA NA NA NA ...
$ TuQuiescentfemales : int NA NA NA NA NA NA NA NA NA NA ...
$ Observations : chr NA NA NA NA ...
$ Block2 : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
$ Rep2 : Factor w/ 5 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Disk2 : Factor w/ 16 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
$ Leaf2 : Factor w/ 2 levels "3","4": 1 2 1 2 1 2 1 2 1 2 ...
$ Env2 : Factor w/ 2 levels "Cd","N": 2 2 1 1 2 2 1 1 2 2 ...
$ FocalSR2 : Factor w/ 4 levels "1","2","4","5": 3 3 3 3 3 3 3 3 3 3 ...
$ CompSR2 : Factor w/ 4 levels "1","2","4","5": NA NA NA NA NA NA NA NA NA NA ...
$ Type2 : Factor w/ 2 levels "INTER","INTRA": 2 2 2 2 2 2 2 2 2 2 ...
$ Focal_Female2 : Factor w/ 2 levels "Te","Tu": 1 1 1 1 1 1 1 1 1 1 ...
$ Nr_Focal_Females_Tu_Alive_G0: num NA NA NA NA NA NA NA NA NA NA ...
$ Nr_Focal_Females_Te_Alive_G0: num 1 1 0 1 1 1 2 2 4 4 ...
$ Num_Comp_Tu_Alive_G0 : num NA NA NA NA NA NA NA NA NA NA ...
$ Num_Comp_Te_Alive_G0 : num NA NA NA NA NA NA NA NA NA NA ...
$ Nr_Focal_Females_G0 : num 1 1 1 1 2 2 2 2 4 4 ...
$ Nr_Comp_Females_G0 : num 0 0 0 0 1 1 1 1 3 3 ...
$ GrowthRateOA : num 6 9 2 5 1 5 2 2 2.25 1.5 ...
$ GrowthRateOA_Q : num 6 9 2 5 1 5 3.5 2 3 2.5 ...
$ Replicate : chr "1" "1" "1" "1" ...
rk_func<- function(lambda, alpha_ii, alpha_ij, dens_i, dens_j, ...){
gr<-lambda*exp(-alpha_ii*dens_i - alpha_ij*dens_j)
return(gr)
}
red_ca_B<-ca[,c("Env", "Rep", "FocalSR", "CompSR", "Dens", "Type", "TeFemales", "TuFemales", "GrowthRateOA")]
red_ca_B
red_ca_B$Dens_Focal<-sapply(c(1:length(red_ca_B[,1])), function(x){
if(red_ca_B$Type[x]=="INTRA"){
a<-red_ca_B$Dens[x]-1
}else if(red_ca_B$Type[x]=="INTER"){
a<-1
}
a
})
red_ca_B$Dens_Comp<-sapply(c(1:length(red_ca_B[,1])), function(x){
if(red_ca_B$Type[x]=="INTRA"){
a<-0
}else if(red_ca_B$Type[x]=="INTER"){
a<-red_ca_B$Dens[x]-1
}
a
})
red_ca_B$Focal<-mapvalues(red_ca_B$FocalSR, c(1,2,4,5), c("SR1", "SR2","SR4", "SR5"))
red_ca_B$Comp<-mapvalues(red_ca_B$CompSR, c(1,2,4,5), c("SR1", "SR2","SR4", "SR5"))
red_ca_B$pred<-sapply(c(1:length(red_ca_B[,1])), function(x){
if(red_ca_B$Focal[x]=="SR1" | red_ca_B$Focal[x]=="SR2"){
aux_data<-subset(param_all_B, Environment==red_ca_B$Env[x] & Replicate== red_ca_B$Rep[x] & as.character(Tu_Regime)==red_ca_B$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Tu_lambda[1], alpha_ii =aux_data$Tu_intra[1], alpha_ij = aux_data$Tu_inter[1], dens_i = red_ca_B$Dens_Focal[x], dens_j = red_ca_B$Dens_Comp[x])
}else if(red_ca_B$Focal[x]=="SR4" | red_ca_B$Focal[x]=="SR5"){
aux_data<-subset(param_all_B, Environment==red_ca_B$Env[x] & Replicate== red_ca_B$Rep[x] & as.character(Te_Regime)==red_ca_B$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Te_lambda[1], alpha_ii =aux_data$Te_intra[1], alpha_ij = aux_data$Te_inter[1], dens_i = red_ca_B$Dens_Focal[x], dens_j = red_ca_B$Dens_Comp[x])
}
aux_pred
})
red_ca_B$pred_L<-sapply(c(1:length(red_ca_B[,1])), function(x){
if(red_ca_B$Focal[x]=="SR1" | red_ca_B$Focal[x]=="SR2"){
aux_data<-subset(param_all_B_lower, Environment==red_ca_B$Env[x] & Replicate== red_ca_B$Rep[x] & as.character(Tu_Regime)==red_ca_B$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Tu_lambda[1], alpha_ii =aux_data$Tu_intra[1], alpha_ij = aux_data$Tu_inter[1], dens_i = red_ca_B$Dens_Focal[x], dens_j = red_ca_B$Dens_Comp[x])
}else if(red_ca_B$Focal[x]=="SR4" | red_ca_B$Focal[x]=="SR5"){
aux_data<-subset(param_all_B_lower, Environment==red_ca_B$Env[x] & Replicate== red_ca_B$Rep[x] & as.character(Te_Regime)==red_ca_B$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Te_lambda[1], alpha_ii =aux_data$Te_intra[1], alpha_ij = aux_data$Te_inter[1], dens_i = red_ca_B$Dens_Focal[x], dens_j = red_ca_B$Dens_Comp[x])
}
aux_pred
})
red_ca_B$pred_U<-sapply(c(1:length(red_ca_B[,1])), function(x){
if(red_ca_B$Focal[x]=="SR1" | red_ca_B$Focal[x]=="SR2"){
aux_data<-subset(param_all_B_upper, Environment==red_ca_B$Env[x] & Replicate== red_ca_B$Rep[x] & as.character(Tu_Regime)==red_ca_B$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Tu_lambda[1], alpha_ii =aux_data$Tu_intra[1], alpha_ij = aux_data$Tu_inter[1], dens_i = red_ca_B$Dens_Focal[x], dens_j = red_ca_B$Dens_Comp[x])
}else if(red_ca_B$Focal[x]=="SR4" | red_ca_B$Focal[x]=="SR5"){
aux_data<-subset(param_all_B_upper, Environment==red_ca_B$Env[x] & Replicate== red_ca_B$Rep[x] & as.character(Te_Regime)==red_ca_B$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Te_lambda[1], alpha_ii =aux_data$Te_intra[1], alpha_ij = aux_data$Te_inter[1], dens_i = red_ca_B$Dens_Focal[x], dens_j = red_ca_B$Dens_Comp[x])
}
aux_pred
})
red_ca_B$Replicate<-red_ca_B$Rep
str(red_ca_B)
'data.frame': 3451 obs. of 17 variables:
$ Env : chr "N" "N" "Cd" "Cd" ...
$ Rep : int 1 1 1 1 1 1 1 1 1 1 ...
$ FocalSR : int 4 4 4 4 4 4 4 4 4 4 ...
$ CompSR : int NA NA NA NA NA NA NA NA NA NA ...
$ Dens : int 1 1 1 1 2 2 2 2 4 4 ...
$ Type : chr "INTRA" "INTRA" "INTRA" "INTRA" ...
$ TeFemales : int 6 9 2 5 2 10 4 4 9 6 ...
$ TuFemales : int NA NA NA NA NA NA NA NA NA NA ...
$ GrowthRateOA: num 6 9 2 5 1 5 2 2 2.25 1.5 ...
$ Dens_Focal : num 0 0 0 0 1 1 1 1 3 3 ...
$ Dens_Comp : num 0 0 0 0 0 0 0 0 0 0 ...
$ Focal : chr "SR4" "SR4" "SR4" "SR4" ...
$ Comp : chr NA NA NA NA ...
$ pred : num 11.11 11.11 3.3 3.3 9.48 ...
$ pred_L : num 6.41 6.41 1.41 1.41 5.58 ...
$ pred_U : num 15.81 15.81 5.19 5.19 13.21 ...
$ Replicate : int 1 1 1 1 1 1 1 1 1 1 ...
ggplot(subset(red_ca_B, Env=="N" & FocalSR==1), aes(x=log(GrowthRateOA+1), y=log(pred+1)))+
facet_grid(CompSR~Replicate)+
geom_abline(slope = 1, intercept=0)+
geom_point()+
ylab("Predicted")+
xlab("Observed")+
ggtitle("Normal environment Control urticae")
ggplot(subset(red_ca_B, Env=="N" & FocalSR==1), aes(x=GrowthRateOA, y=pred))+
facet_grid(CompSR~Replicate)+
geom_abline(slope = 1, intercept=0)+
geom_point()+
ylab("Predicted")+
xlab("Observed")+
ggtitle("Normal environment Control urticae")
ggplot(subset(red_ca_B, Env=="N" & FocalSR==1 & Type=="INTRA"), aes(y=GrowthRateOA, x=Dens_Focal))+
facet_grid(.~Replicate)+
geom_point(position=position_dodge2(0.5))+
geom_line(aes(y=pred, x=Dens_Focal))+
geom_ribbon(aes(ymin=pred_L, ymax=pred_U), fill="lightgrey", linetype="dashed", colour="black")+
ylab("Growth rate")+
xlab("Density")+
ggtitle("Normal environment Control urticae Intra")
ggplot(subset(red_ca_B, Env=="N" & FocalSR==1 & Type=="INTER" & CompSR=="4"), aes(y=GrowthRateOA, x=Dens_Comp))+
facet_grid(.~Replicate)+
geom_point(position=position_dodge2(0.5))+
geom_abline(data=subset(param_all_B,Tu_Regime=="SR1" & Environment=="N" & Te_Regime=="SR4"), aes(slope=-Tu_inter, intercept = Tu_lambda))+
ylab("Growth rate")+
xlab("Density")+
ggtitle("Normal environment Control urticae Inter Control")
ggplot(subset(red_ca_B, Env=="N" & FocalSR==1 & Type=="INTER" & CompSR=="5"), aes(y=GrowthRateOA, x=Dens_Comp))+
facet_grid(.~Replicate)+
geom_point(position=position_dodge2(0.5))+
geom_abline(data=subset(param_all_B,Tu_Regime=="SR1" & Environment=="N" & Te_Regime=="SR5"), aes(slope=-Tu_inter, intercept = Tu_lambda))+
ylab("Growth rate")+
xlab("Density")+
ggtitle("Normal environment Control urticae Inter Evolved")
NA
NA
To do this we have to trick the cxr (Oscar suggestion), by putting the intraspecific competitors in another column than the focal and then estimate only intra!
dir.create("./NewAnalyses/cxr_lambda_fixed_nested", showWarnings = FALSE)
# modifying data frame to fit the type of setup that is need for CXR
CXR_C_N<-subset(ca, Env=="N")[,c("Rep", "FocalSR", "CompSR", "Dens", "TeFemales", "TuFemales")]
CXR_C_N$Focal<-mapvalues(CXR_C_N$FocalSR, c(1,2,4,5), c("SR1", "SR2","SR4","SR5"))
CXR_C_N$CompSR2<-mapvalues(CXR_C_N$CompSR, c(1,2,4,5), c("SR1", "SR2","SR4","SR5"))
CXR_C_N$Comp<-sapply(c(1:length(CXR_C_N[,1])), function(x){
if(is.na(CXR_C_N$CompSR2[x])){
a<- CXR_C_N$Focal[x]
}else{
a<-CXR_C_N$CompSR2[x]
}
a
})
aux<-data.frame(SR1=rep(0, length(CXR_C_N[,1])), SR2=rep(0, length(CXR_C_N[,1])), SR4=rep(0, length(CXR_C_N[,1])), SR5=rep(0, length(CXR_C_N[,1])))
for(i in 1:length(CXR_C_N[,1])){
#coluna onde por focais
colunaF<-which(colnames(aux)==CXR_C_N$Focal[i])
#coluna onde por competidors
colunaC<-which(colnames(aux)==CXR_C_N$Comp[i])
#if its the same regime
if(CXR_C_N$Focal[i]==CXR_C_N$Comp[i] & CXR_C_N$Dens[i]==1){
aux[i,colunaF]<-CXR_C_N$Dens[i]-1
}else if(CXR_C_N$Focal[i]==CXR_C_N$Comp[i]){
aux[i,colunaF]<-CXR_C_N$Dens[i]-1
}else{ #if it is heterospecific then its -1 for the competitors (because of the focal) and its one for the focal
aux[i,colunaC]<-CXR_C_N$Dens[i]-1
aux[i, colunaF]<-1
}
}
CXR_C_N<-cbind(CXR_C_N, aux)
CXR_C_N$fitness<-sapply(c(1:length(CXR_C_N[,1])), function(x){
colF<-which(colnames(CXR_C_N)==CXR_C_N$Focal[x])
if(CXR_C_N$Focal[x]=="SR1"){
a<-CXR_C_N$TuFemales[x]/CXR_C_N$SR1[x]
} else if(CXR_C_N$Focal[x]=="SR2"){
a<-CXR_C_N$TuFemales[x]/CXR_C_N$SR2[x]
} else if(CXR_C_N$Focal[x]=="SR4"){
a<-CXR_C_N$TeFemales[x]/CXR_C_N$SR4[x]
} else if(CXR_C_N$Focal[x]=="SR5"){
a<-CXR_C_N$TeFemales[x]/CXR_C_N$SR5[x]
}
a
})
#removing rows for which there is no data for fitness
CXR_C_N<-CXR_C_N[-which(is.na(CXR_C_N$fitness)),]
# adding +1 to all data
#CXR_C_N$fitness<-CXR_C_N$fitness+1
CXR_C_N[which(CXR_C_N$fitness=="-Inf" | CXR_C_N$fitness=="Inf"),"fitness"]<-0
# all data gets +1 because of the 0 problem
CXR_C_N$fitness<-CXR_C_N$fitness+1
# vector that tells which are the selection regimes, the columns have to have the same name
my.reg <- c("SR1", "SR2","SR4","SR5")
str(CXR_C_N)
'data.frame': 1637 obs. of 14 variables:
$ Rep : int 1 1 1 1 1 1 1 1 1 1 ...
$ FocalSR : int 4 4 4 4 4 4 4 4 4 4 ...
$ CompSR : int NA NA NA NA NA NA NA NA NA NA ...
$ Dens : int 1 1 2 2 4 4 10 10 1 1 ...
$ TeFemales: int 6 9 2 10 9 6 13 48 11 13 ...
$ TuFemales: int NA NA NA NA NA NA NA NA NA NA ...
$ Focal : chr "SR4" "SR4" "SR4" "SR4" ...
$ CompSR2 : chr NA NA NA NA ...
$ Comp : chr "SR4" "SR4" "SR4" "SR4" ...
$ SR1 : num 0 0 0 0 0 0 0 0 0 0 ...
$ SR2 : num 0 0 0 0 0 0 0 0 0 0 ...
$ SR4 : num 0 0 1 1 3 3 9 9 0 0 ...
$ SR5 : num 0 0 0 0 0 0 0 0 0 0 ...
$ fitness : num 1 1 3 11 4 ...
# Do list per replicate and environment
R1_intra<-list(SR1= subset(CXR_C_N, Rep==1 & Focal=="SR1" & Comp=="SR1")[,c("fitness", "SR1")], SR2= subset(CXR_C_N, Rep==1 & Focal=="SR2" & Comp=="SR2")[,c("fitness", "SR2")], SR4= subset(CXR_C_N, Rep==1 & Focal=="SR4" & Comp=="SR4")[,c("fitness", "SR4")], SR5= subset(CXR_C_N, Rep==1 & Focal=="SR5" & Comp=="SR5")[,c("fitness", "SR5")])
R2_intra<-list(SR1= subset(CXR_C_N, Rep==2 & Focal=="SR1" & Comp=="SR1" )[,c("fitness", "SR1")], SR4= subset(CXR_C_N, Rep==2 & Focal=="SR4" & Comp=="SR4")[,c("fitness", "SR4")], SR5= subset(CXR_C_N, Rep==2 & Focal=="SR5" & Comp=="SR5")[,c("fitness", "SR5")])
R3_intra<-list(SR1= subset(CXR_C_N, Rep==3 & Focal=="SR1" & Comp=="SR1" )[,c("fitness", "SR1")], SR2= subset(CXR_C_N, Rep==3 & Focal=="SR2" & Comp=="SR2")[,c("fitness", "SR2")], SR4= subset(CXR_C_N, Rep==3 & Focal=="SR4" & Comp=="SR4")[,c("fitness", "SR4")], SR5= subset(CXR_C_N, Rep==3 & Focal=="SR5" & Comp=="SR5")[,c("fitness", "SR5")])
R4_intra<-list(SR1= subset(CXR_C_N, Rep==4 & Focal=="SR1" & Comp=="SR1" )[,c("fitness", "SR1")], SR2= subset(CXR_C_N, Rep==4 & Focal=="SR2" & Comp=="SR2")[,c("fitness", "SR2")], SR4= subset(CXR_C_N, Rep==4 & Focal=="SR4" & Comp=="SR4")[,c("fitness", "SR4")], SR5= subset(CXR_C_N, Rep==4 & Focal=="SR5" & Comp=="SR5")[,c("fitness", "SR5")])
R5_intra<-list(SR1= subset(CXR_C_N, Rep==5 & Focal=="SR1" & Comp=="SR1" )[,c("fitness", "SR1")], SR2= subset(CXR_C_N, Rep==5 & Focal=="SR2" & Comp=="SR2")[,c("fitness", "SR2")], SR4= subset(CXR_C_N, Rep==5 & Focal=="SR4" & Comp=="SR4")[,c("fitness", "SR4")], SR5= subset(CXR_C_N, Rep==5 & Focal=="SR5" & Comp=="SR5")[,c("fitness", "SR5")])
####################################
######## DOING THE INTRA ESTIMATES
###################################
cxr_C.R1_intra<-cxr_pm_multifit(data = R1_intra,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_1N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R2_intra<-cxr_pm_multifit(data = R2_intra,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_2N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R3_intra<-cxr_pm_multifit(data = R3_intra,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_3N,
# no standard errors
bootstrap_samples = 200)
Warning: NaNs producedWarning: NaNs producedWarning: NaNs producedWarning: NaNs producedWarning: NaNs producedWarning: NaNs produced
cxr_C.R4_intra<-cxr_pm_multifit(data = R4_intra,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_4N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R5_intra<-cxr_pm_multifit(data = R5_intra,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_5N,
# no standard errors
bootstrap_samples = 200)
summary(cxr_C.R1_intra)
model: 'RK_pm_alpha_global_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
alpha
SR1 0.1091347
SR2 0.0764843
SR4 0.1439655
SR5 0.1236622
#################
######Doing the inter estimates
##################
R1<-list(SR1= subset(CXR_C_N, Rep==1 & Focal=="SR1" & Comp!="SR1")[,c("fitness", "SR4", "SR5")], SR2= subset(CXR_C_N, Rep==1 & Focal=="SR2"& Comp!="SR2")[,c("fitness", "SR4", "SR5")], SR4= subset(CXR_C_N, Rep==1 & Focal=="SR4" & Comp!="SR4")[,c("fitness", "SR1", "SR2")], SR5= subset(CXR_C_N, Rep==1 & Focal=="SR5" & Comp!="SR5")[,c("fitness", "SR1", "SR2")])
R2<-list(SR1= subset(CXR_C_N, Rep==2 & Focal=="SR1" & Comp!="SR1")[,c("fitness", "SR4", "SR5")], SR4= subset(CXR_C_N, Rep==2 & Focal=="SR4" & Comp!="SR4")[,c("fitness", "SR1")], SR5= subset(CXR_C_N, Rep==2 & Focal=="SR5" & Comp!="SR5")[,c("fitness", "SR1")])
R3<-list(SR1= subset(CXR_C_N, Rep==3 & Focal=="SR1" & Comp!="SR1")[,c("fitness", "SR4", "SR5")], SR2= subset(CXR_C_N, Rep==3 & Focal=="SR2" & Comp!="SR2")[,c("fitness", "SR4", "SR5")], SR4= subset(CXR_C_N, Rep==3 & Focal=="SR4" & Comp!="SR4")[,c("fitness", "SR1", "SR2")], SR5= subset(CXR_C_N, Rep==3 & Focal=="SR5" & Comp!="SR5")[,c("fitness", "SR1", "SR2")])
R4<-list(SR1= subset(CXR_C_N, Rep==4 & Focal=="SR1" & Comp!="SR1")[,c("fitness", "SR4", "SR5")], SR2= subset(CXR_C_N, Rep==4 & Focal=="SR2" & Comp!="SR2")[,c("fitness", "SR4", "SR5")], SR4= subset(CXR_C_N, Rep==4 & Focal=="SR4" & Comp!="SR4")[,c("fitness", "SR1", "SR2")], SR5= subset(CXR_C_N, Rep==4 & Focal=="SR5" & Comp!="SR5")[,c("fitness", "SR1", "SR2")])
R5<-list(SR1= subset(CXR_C_N, Rep==5 & Focal=="SR1" & Comp!="SR1")[,c("fitness", "SR4", "SR5")], SR2= subset(CXR_C_N, Rep==5 & Focal=="SR2" & Comp!="SR2")[,c("fitness", "SR4", "SR5")], SR4= subset(CXR_C_N, Rep==5 & Focal=="SR4" & Comp!="SR4")[,c("fitness", "SR1", "SR2")], SR5= subset(CXR_C_N, Rep==5 & Focal=="SR5" & Comp!="SR5")[,c("fitness", "SR1", "SR2")])
cxr_C.R1<-cxr_pm_multifit(data = R1,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_1N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R2_sr1<-cxr_pm_fit(data = R2[[1]],
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_2N[[1]],
# no standard errors
bootstrap_samples = 200)
cxr_C.R2_sr4<-cxr_pm_fit(data = R2[[2]],
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_2N[[2]],
# no standard errors
bootstrap_samples = 200)
cxr_C.R2_sr5<-cxr_pm_fit(data = R2[[3]],
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_2N[[3]],
# no standard errors
bootstrap_samples = 200)
cxr_C.R3<-cxr_pm_multifit(data = R3,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_3N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R4<-cxr_pm_multifit(data = R4,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_4N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R5<-cxr_pm_multifit(data = R5,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_5N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R2_intra$alpha_matrix
alpha
SR1 0.10677055
SR4 0.06945784
SR5 0.14419887
# modifying data frame to fit the type of setup that is need for CXR
CXR_C_Cd<-subset(ca, Env=="Cd")[,c("Rep", "FocalSR", "CompSR", "Dens", "TeFemales", "TuFemales")]
CXR_C_Cd$Focal<-mapvalues(CXR_C_Cd$FocalSR, c(1,2,4,5), c("SR1", "SR2","SR4","SR5"))
CXR_C_Cd$CompSR2<-mapvalues(CXR_C_Cd$CompSR, c(1,2,4,5), c("SR1", "SR2","SR4","SR5"))
CXR_C_Cd$Comp<-sapply(c(1:length(CXR_C_Cd[,1])), function(x){
if(is.na(CXR_C_Cd$CompSR2[x])){
a<- CXR_C_Cd$Focal[x]
}else{
a<-CXR_C_Cd$CompSR2[x]
}
a
})
aux<-data.frame(SR1=rep(0, length(CXR_C_Cd[,1])), SR2=rep(0, length(CXR_C_Cd[,1])), SR4=rep(0, length(CXR_C_Cd[,1])), SR5=rep(0, length(CXR_C_Cd[,1])))
for(i in 1:length(CXR_C_Cd[,1])){
#coluna onde por focais
colunaF<-which(colnames(aux)==CXR_C_Cd$Focal[i])
#coluna onde por competidors
colunaC<-which(colnames(aux)==CXR_C_Cd$Comp[i])
#if its the same regime
if(CXR_C_Cd$Focal[i]==CXR_C_Cd$Comp[i] & CXR_C_Cd$Dens[i]==1){
aux[i,colunaF]<-CXR_C_Cd$Dens[i]-1
}else if(CXR_C_Cd$Focal[i]==CXR_C_Cd$Comp[i]){
aux[i,colunaF]<-CXR_C_Cd$Dens[i]-1
}else{ #if it is heterospecific then its -1 for the competitors (because of the focal) and its one for the focal
aux[i,colunaC]<-CXR_C_Cd$Dens[i]-1
aux[i, colunaF]<-1
}
}
CXR_C_Cd<-cbind(CXR_C_Cd, aux)
CXR_C_Cd$fitness<-sapply(c(1:length(CXR_C_Cd[,1])), function(x){
colF<-which(colnames(CXR_C_Cd)==CXR_C_Cd$Focal[x])
if(CXR_C_Cd$Focal[x]=="SR1"){
a<-CXR_C_Cd$TuFemales[x]/CXR_C_Cd$SR1[x]
} else if(CXR_C_Cd$Focal[x]=="SR2"){
a<-CXR_C_Cd$TuFemales[x]/CXR_C_Cd$SR2[x]
} else if(CXR_C_Cd$Focal[x]=="SR4"){
a<-CXR_C_Cd$TeFemales[x]/CXR_C_Cd$SR4[x]
} else if(CXR_C_Cd$Focal[x]=="SR5"){
a<-CXR_C_Cd$TeFemales[x]/CXR_C_Cd$SR5[x]
}
a
})
#removing rows for which there is no data for fitness
CXR_C_Cd<-CXR_C_Cd[-which(is.na(CXR_C_Cd$fitness)),]
# adding +1 to all data
#CXR_C_Cd$fitness<-CXR_C_Cd$fitness+1
CXR_C_Cd[which(CXR_C_Cd$fitness=="-Inf" | CXR_C_Cd$fitness=="Inf"),"fitness"]<-0
# all data gets +1 because of the 0 problem
CXR_C_Cd$fitness<-CXR_C_Cd$fitness+1
# vector that tells which are the selection regimes, the columns have to have the same name
my.reg <- c("SR1", "SR2","SR4","SR5")
str(CXR_C_Cd)
'data.frame': 1585 obs. of 14 variables:
$ Rep : int 1 1 1 1 1 1 1 1 1 1 ...
$ FocalSR : int 4 4 4 4 4 4 4 4 4 4 ...
$ CompSR : int NA NA NA NA NA NA NA NA NA NA ...
$ Dens : int 1 1 2 2 4 4 10 10 1 1 ...
$ TeFemales: int 2 5 4 4 0 0 7 3 2 1 ...
$ TuFemales: int NA NA NA NA NA NA NA NA NA NA ...
$ Focal : chr "SR4" "SR4" "SR4" "SR4" ...
$ CompSR2 : chr NA NA NA NA ...
$ Comp : chr "SR4" "SR4" "SR4" "SR4" ...
$ SR1 : num 0 0 0 0 0 0 0 0 0 0 ...
$ SR2 : num 0 0 0 0 0 0 0 0 0 0 ...
$ SR4 : num 0 0 1 1 3 3 9 9 0 0 ...
$ SR5 : num 0 0 0 0 0 0 0 0 0 0 ...
$ fitness : num 1 1 5 5 1 ...
# Do list per replicate and environment
R1_cd_intra<-list(SR1= subset(CXR_C_Cd, Rep==1 & Focal=="SR1" & Comp=="SR1")[,c("fitness", "SR1")], SR2= subset(CXR_C_Cd, Rep==1 & Focal=="SR2" & Comp=="SR2")[,c("fitness", "SR2")], SR4= subset(CXR_C_Cd, Rep==1 & Focal=="SR4" & Comp=="SR4")[,c("fitness", "SR4")], SR5= subset(CXR_C_Cd, Rep==1 & Focal=="SR5" & Comp=="SR5")[,c("fitness", "SR5")])
R2_cd_intra<-list(SR1= subset(CXR_C_Cd, Rep==2 & Focal=="SR1" & Comp=="SR1" )[,c("fitness", "SR1")], SR4= subset(CXR_C_Cd, Rep==2 & Focal=="SR4" & Comp=="SR4")[,c("fitness", "SR4")], SR5= subset(CXR_C_Cd, Rep==2 & Focal=="SR5" & Comp=="SR5")[,c("fitness", "SR5")])
R3_cd_intra<-list(SR1= subset(CXR_C_Cd, Rep==3 & Focal=="SR1" & Comp=="SR1" )[,c("fitness", "SR1")], SR2= subset(CXR_C_Cd, Rep==3 & Focal=="SR2" & Comp=="SR2")[,c("fitness", "SR2")], SR4= subset(CXR_C_Cd, Rep==3 & Focal=="SR4" & Comp=="SR4")[,c("fitness", "SR4")], SR5= subset(CXR_C_Cd, Rep==3 & Focal=="SR5" & Comp=="SR5")[,c("fitness", "SR5")])
R4_cd_intra<-list(SR1= subset(CXR_C_Cd, Rep==4 & Focal=="SR1" & Comp=="SR1" )[,c("fitness", "SR1")], SR2= subset(CXR_C_Cd, Rep==4 & Focal=="SR2" & Comp=="SR2")[,c("fitness", "SR2")], SR4= subset(CXR_C_Cd, Rep==4 & Focal=="SR4" & Comp=="SR4")[,c("fitness", "SR4")], SR5= subset(CXR_C_Cd, Rep==4 & Focal=="SR5" & Comp=="SR5")[,c("fitness", "SR5")])
R5_cd_intra<-list(SR1= subset(CXR_C_Cd, Rep==5 & Focal=="SR1" & Comp=="SR1" )[,c("fitness", "SR1")], SR2= subset(CXR_C_Cd, Rep==5 & Focal=="SR2" & Comp=="SR2")[,c("fitness", "SR2")], SR4= subset(CXR_C_Cd, Rep==5 & Focal=="SR4" & Comp=="SR4")[,c("fitness", "SR4")], SR5= subset(CXR_C_Cd, Rep==5 & Focal=="SR5" & Comp=="SR5")[,c("fitness", "SR5")])
#### lambda
fixed_terms_C_1N <- list(list(lambda = subset(mean_dens1, Rep==1 & Env=="Cd" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==1 & Env=="Cd" & SR==2)$lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==1 & Env=="Cd" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==1 & Env=="Cd" & SR==5)$lambda))
fixed_terms_C_2N <- list(list(lambda = subset(mean_dens1, Rep==2 & Env=="Cd" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==2 & Env=="Cd" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==2 & Env=="Cd" & SR==5)$lambda))
fixed_terms_C_3N <- list(list(lambda = subset(mean_dens1, Rep==3 & Env=="Cd" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==3 & Env=="Cd" & SR==2)$lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==3 & Env=="Cd" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==3 & Env=="Cd" & SR==5)$lambda))
fixed_terms_C_4N <- list(list(lambda = subset(mean_dens1, Rep==4 & Env=="Cd" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==4 & Env=="Cd" & SR==2)$lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==4 & Env=="Cd" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==4 & Env=="Cd" & SR==5)$lambda))
fixed_terms_C_5N <- list(list(lambda = subset(mean_dens1, Rep==5 & Env=="Cd" & SR==1)$lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==5 & Env=="Cd" & SR==2)$lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==5 & Env=="Cd" & SR==4)$lambda),
list(lambda= subset(mean_dens1, Rep==5 & Env=="Cd" & SR==5)$lambda))
####################################
######## DOING THE INTRA ESTIMATES
###################################
cxr_C.R1_cd_intra<-cxr_pm_multifit(data = R1_cd_intra,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_C_1N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R2_cd_intra<-cxr_pm_multifit(data = R2_cd_intra,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_C_2N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R3_cd_intra<-cxr_pm_multifit(data = R3_cd_intra,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_C_3N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R4_cd_intra<-cxr_pm_multifit(data = R4_cd_intra,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_C_4N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R5_cd_intra<-cxr_pm_multifit(data = R5_cd_intra,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_C_5N,
# no standard errors
bootstrap_samples = 200)
summary(cxr_C.R1_cd_intra)
model: 'RK_pm_alpha_global_lambdacov_none_alphacov_none'
optimization method: 'Nelder-Mead'
----------
----------
alpha matrix:
alpha
SR1 0.03859441
SR2 0.06428340
SR4 0.09173854
SR5 0.14814401
#################
######Doing the inter estimates
##################
R1_cd<-list(SR1= subset(CXR_C_Cd, Rep==1 & Focal=="SR1" & Comp!="SR1")[,c("fitness", "SR4", "SR5")], SR2= subset(CXR_C_Cd, Rep==1 & Focal=="SR2"& Comp!="SR2")[,c("fitness", "SR4", "SR5")], SR4= subset(CXR_C_Cd, Rep==1 & Focal=="SR4" & Comp!="SR4")[,c("fitness", "SR1", "SR2")], SR5= subset(CXR_C_Cd, Rep==1 & Focal=="SR5" & Comp!="SR5")[,c("fitness", "SR1", "SR2")])
R2_cd<-list(SR1= subset(CXR_C_Cd, Rep==2 & Focal=="SR1" & Comp!="SR1")[,c("fitness", "SR4", "SR5")], SR4= subset(CXR_C_Cd, Rep==2 & Focal=="SR4" & Comp!="SR4")[,c("fitness", "SR1")], SR5= subset(CXR_C_Cd, Rep==2 & Focal=="SR5" & Comp!="SR5")[,c("fitness", "SR1")])
R3_cd<-list(SR1= subset(CXR_C_Cd, Rep==3 & Focal=="SR1" & Comp!="SR1")[,c("fitness", "SR4", "SR5")], SR2= subset(CXR_C_Cd, Rep==3 & Focal=="SR2" & Comp!="SR2")[,c("fitness", "SR4", "SR5")], SR4= subset(CXR_C_Cd, Rep==3 & Focal=="SR4" & Comp!="SR4")[,c("fitness", "SR1", "SR2")], SR5= subset(CXR_C_Cd, Rep==3 & Focal=="SR5" & Comp!="SR5")[,c("fitness", "SR1", "SR2")])
R4_cd<-list(SR1= subset(CXR_C_Cd, Rep==4 & Focal=="SR1" & Comp!="SR1")[,c("fitness", "SR4", "SR5")], SR2= subset(CXR_C_Cd, Rep==4 & Focal=="SR2" & Comp!="SR2")[,c("fitness", "SR4", "SR5")], SR4= subset(CXR_C_Cd, Rep==4 & Focal=="SR4" & Comp!="SR4")[,c("fitness", "SR1", "SR2")], SR5= subset(CXR_C_Cd, Rep==4 & Focal=="SR5" & Comp!="SR5")[,c("fitness", "SR1", "SR2")])
R5_cd<-list(SR1= subset(CXR_C_Cd, Rep==5 & Focal=="SR1" & Comp!="SR1")[,c("fitness", "SR4", "SR5")], SR2= subset(CXR_C_Cd, Rep==5 & Focal=="SR2" & Comp!="SR2")[,c("fitness", "SR4", "SR5")], SR4= subset(CXR_C_Cd, Rep==5 & Focal=="SR4" & Comp!="SR4")[,c("fitness", "SR1", "SR2")], SR5= subset(CXR_C_Cd, Rep==5 & Focal=="SR5" & Comp!="SR5")[,c("fitness", "SR1", "SR2")])
cxr_C.R1_cd<-cxr_pm_multifit(data = R1_cd,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_C_1N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R2_cd_sr1<-cxr_pm_fit(data = R2_cd[[1]],
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_C_2N[[1]],
# no standard errors
bootstrap_samples = 200)
cxr_C.R2_cd_sr4<-cxr_pm_fit(data = R2_cd[[2]],
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_C_2N[[2]],
# no standard errors
bootstrap_samples = 200)
cxr_C.R2_cd_sr5<-cxr_pm_fit(data = R2_cd[[3]],
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "global",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_C_2N[[3]],
# no standard errors
bootstrap_samples = 200)
cxr_C.R3_cd<-cxr_pm_multifit(data = R3_cd,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_C_3N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R4_cd<-cxr_pm_multifit(data = R4_cd,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_C_4N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R5_cd<-cxr_pm_multifit(data = R5_cd,
focal_column = NULL,
model_family = "RK",
covariates = NULL,
optimization_method = "Nelder-Mead",
alpha_form = "pairwise",
lambda_cov_form = "none",
alpha_cov_form = "none",
initial_values = list(alpha_inter = 0.1),
fixed_terms = fixed_terms_C_5N,
# no standard errors
bootstrap_samples = 200)
cxr_C.R1_cd$alpha_matrix
SR1 SR2 SR4 SR5
SR1 NA NA 0.06242575 0.05005673
SR2 NA NA 0.05033948 0.05637467
SR4 0.05321877 0.08501578 NA NA
SR5 0.13982261 0.11477131 NA NA
rows in the alpha element of the returning list correspond to species i and columns to species j for each αij coefficient.
cxr_param_C<-expand.grid(Tu_Regime=c("SR1", "SR2"), Te_Regime=c("SR4", "SR5"), Replicate=c(1,2,3,4,5), Environment=c("N"))
cxr_param_C$Tu_lambda<-0
cxr_param_C$Te_lambda<-0
cxr_param_C$Tu_intra<-0
cxr_param_C$Te_intra<-0
cxr_param_C$Tu_inter<-0
cxr_param_C$Te_inter<-0
#removing SR2 for replicate 2
cxr_param_C<-cxr_param_C[-which(cxr_param_C$Replicate==2 & cxr_param_C$Tu_Regime=="SR2"),]
cxr_param_C[which(cxr_param_C$Replicate==1),"Tu_lambda"]<-c(cxr_C.R1_intra$fixed_terms[[1]]$lambda,cxr_C.R1_intra$fixed_terms[[2]]$lambda)
cxr_param_C[which(cxr_param_C$Replicate==1),"Te_lambda"]<-c(cxr_C.R1_intra$fixed_terms[[3]]$lambda,cxr_C.R1_intra$fixed_terms[[3]]$lambda, cxr_C.R1_intra$fixed_terms[[4]]$lambda,cxr_C.R1_intra$fixed_terms[[4]]$lambda)
cxr_param_C[which(cxr_param_C$Replicate==2),"Tu_lambda"]<-c(cxr_C.R2_intra$fixed_terms[[1]]$lambda,cxr_C.R2_intra$fixed_terms[[1]]$lambda)
cxr_param_C[which(cxr_param_C$Replicate==2),"Te_lambda"]<-c(cxr_C.R2_intra$fixed_terms[[2]]$lambda,cxr_C.R2_intra$fixed_terms[[3]]$lambda)
cxr_param_C[which(cxr_param_C$Replicate==3),"Tu_lambda"]<-c(cxr_C.R3_intra$fixed_terms[[1]]$lambda,cxr_C.R3_intra$fixed_terms[[2]]$lambda)
cxr_param_C[which(cxr_param_C$Replicate==3),"Te_lambda"]<-c(cxr_C.R3_intra$fixed_terms[[3]]$lambda,cxr_C.R3_intra$fixed_terms[[3]]$lambda, cxr_C.R3_intra$fixed_terms[[4]]$lambda,cxr_C.R3_intra$fixed_terms[[4]]$lambda)
cxr_param_C[which(cxr_param_C$Replicate==4),"Tu_lambda"]<-c(cxr_C.R4_intra$fixed_terms[[1]]$lambda,cxr_C.R4_intra$fixed_terms[[2]]$lambda)
cxr_param_C[which(cxr_param_C$Replicate==4),"Te_lambda"]<-c(cxr_C.R4_intra$fixed_terms[[3]]$lambda,cxr_C.R4_intra$fixed_terms[[3]]$lambda, cxr_C.R4_intra$fixed_terms[[4]]$lambda,cxr_C.R4_intra$fixed_terms[[4]]$lambda)
cxr_param_C[which(cxr_param_C$Replicate==5),"Tu_lambda"]<-c(cxr_C.R5_intra$fixed_terms[[1]]$lambda,cxr_C.R5_intra$fixed_terms[[2]]$lambda)
cxr_param_C[which(cxr_param_C$Replicate==5),"Te_lambda"]<-c(cxr_C.R5_intra$fixed_terms[[3]]$lambda,cxr_C.R5_intra$fixed_terms[[3]]$lambda, cxr_C.R5_intra$fixed_terms[[4]]$lambda,cxr_C.R5_intra$fixed_terms[[4]]$lambda)
cxr_param_C[which(cxr_param_C$Replicate==1),"Tu_intra"]<-rep(c(cxr_C.R1_intra$alpha_matrix[1,1], cxr_C.R1_intra$alpha_matrix[2,1]), 2)
cxr_param_C[which(cxr_param_C$Replicate==1),"Te_intra"]<-rep(c(cxr_C.R1_intra$alpha_matrix[3,1], cxr_C.R1_intra$alpha_matrix[4,1]), each=2)
cxr_param_C[which(cxr_param_C$Replicate==2),"Tu_intra"]<-rep(c(cxr_C.R2_intra$alpha_matrix[1,1]), 2)
cxr_param_C[which(cxr_param_C$Replicate==2),"Te_intra"]<-rep(c(cxr_C.R2_intra$alpha_matrix[2,1], cxr_C.R2_intra$alpha_matrix[3,1]))
cxr_param_C[which(cxr_param_C$Replicate==3),"Tu_intra"]<-rep(c(cxr_C.R3_intra$alpha_matrix[1,1], cxr_C.R3_intra$alpha_matrix[2,1]), 2)
cxr_param_C[which(cxr_param_C$Replicate==3),"Te_intra"]<-rep(c(cxr_C.R3_intra$alpha_matrix[3,1], cxr_C.R3_intra$alpha_matrix[4,1]), each=2)
cxr_param_C[which(cxr_param_C$Replicate==4),"Tu_intra"]<-rep(c(cxr_C.R4_intra$alpha_matrix[1,1], cxr_C.R4_intra$alpha_matrix[2,1]), 2)
cxr_param_C[which(cxr_param_C$Replicate==4),"Te_intra"]<-rep(c(cxr_C.R4_intra$alpha_matrix[3,1], cxr_C.R4_intra$alpha_matrix[4,1]), each=2)
cxr_param_C[which(cxr_param_C$Replicate==5),"Tu_intra"]<-rep(c(cxr_C.R5_intra$alpha_matrix[1,1], cxr_C.R5_intra$alpha_matrix[2,1]), 2)
cxr_param_C[which(cxr_param_C$Replicate==5),"Te_intra"]<-rep(c(cxr_C.R5_intra$alpha_matrix[3,1], cxr_C.R5_intra$alpha_matrix[4,1]), each=2)
cxr_param_C[which(cxr_param_C$Replicate==1),"Tu_inter"]<-c(cxr_C.R1$alpha_matrix[1,3], cxr_C.R1$alpha_matrix[2,3],cxr_C.R1$alpha_matrix[1,4], cxr_C.R1$alpha_matrix[2,4])
cxr_param_C[which(cxr_param_C$Replicate==1),"Te_inter"]<-c(cxr_C.R1$alpha_matrix[3,1], cxr_C.R1$alpha_matrix[3,2],cxr_C.R1$alpha_matrix[4,1], cxr_C.R1$alpha_matrix[4,2])
cxr_param_C[which(cxr_param_C$Replicate==2),"Tu_inter"]<-c(cxr_C.R2_sr1$alpha_inter[1], cxr_C.R2_sr1$alpha_inter[2])
cxr_param_C[which(cxr_param_C$Replicate==2),"Te_inter"]<-c(cxr_C.R2_sr4$alpha_inter[1],cxr_C.R2_sr1$alpha_inter[1])
cxr_param_C[which(cxr_param_C$Replicate==3),"Tu_inter"]<-c(cxr_C.R3$alpha_matrix[1,3], cxr_C.R3$alpha_matrix[2,3],cxr_C.R3$alpha_matrix[1,4], cxr_C.R3$alpha_matrix[2,4])
cxr_param_C[which(cxr_param_C$Replicate==3),"Te_inter"]<-c(cxr_C.R3$alpha_matrix[3,1], cxr_C.R3$alpha_matrix[3,2],cxr_C.R3$alpha_matrix[4,1], cxr_C.R3$alpha_matrix[4,2])
cxr_param_C[which(cxr_param_C$Replicate==4),"Tu_inter"]<-c(cxr_C.R4$alpha_matrix[1,3], cxr_C.R4$alpha_matrix[2,3],cxr_C.R4$alpha_matrix[1,4], cxr_C.R4$alpha_matrix[2,4])
cxr_param_C[which(cxr_param_C$Replicate==4),"Te_inter"]<-c(cxr_C.R4$alpha_matrix[3,1], cxr_C.R4$alpha_matrix[3,2],cxr_C.R4$alpha_matrix[4,1], cxr_C.R4$alpha_matrix[4,2])
cxr_param_C[which(cxr_param_C$Replicate==5),"Tu_inter"]<-c(cxr_C.R5$alpha_matrix[1,3], cxr_C.R5$alpha_matrix[2,3],cxr_C.R5$alpha_matrix[1,4], cxr_C.R5$alpha_matrix[2,4])
cxr_param_C[which(cxr_param_C$Replicate==5),"Te_inter"]<-c(cxr_C.R5$alpha_matrix[3,1], cxr_C.R5$alpha_matrix[3,2],cxr_C.R5$alpha_matrix[4,1], cxr_C.R5$alpha_matrix[4,2])
### Lower
cxr_param_C_lower<-expand.grid(Tu_Regime=c("SR1", "SR2"), Te_Regime=c("SR4", "SR5"), Replicate=c(1,2,3,4,5), Environment=c("N"))
cxr_param_C_lower$Tu_lambda<-0
cxr_param_C_lower$Te_lambda<-0
cxr_param_C_lower$Tu_intra<-0
cxr_param_C_lower$Te_intra<-0
cxr_param_C_lower$Tu_inter<-0
cxr_param_C_lower$Te_inter<-0
#removing SR2 for replicate 2
cxr_param_C_lower<-cxr_param_C_lower[-which(cxr_param_C_lower$Replicate==2 & cxr_param_C_lower$Tu_Regime=="SR2"),]
#Since the error comes directly from the data we need to create some lists with that information
sd_1N <- list(list(lambda = subset(mean_dens1, Rep==1 & Env=="N" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==1 & Env=="N" & SR==2)$sd_lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==1 & Env=="N" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==1 & Env=="N" & SR==5)$sd_lambda))
sd_2N <- list(list(lambda = subset(mean_dens1, Rep==2 & Env=="N" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==2 & Env=="N" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==2 & Env=="N" & SR==5)$sd_lambda))
sd_3N <- list(list(lambda = subset(mean_dens1, Rep==3 & Env=="N" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==3 & Env=="N" & SR==2)$sd_lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==3 & Env=="N" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==3 & Env=="N" & SR==5)$sd_lambda))
sd_4N <- list(list(lambda = subset(mean_dens1, Rep==4 & Env=="N" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==4 & Env=="N" & SR==2)$sd_lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==4 & Env=="N" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==4 & Env=="N" & SR==5)$sd_lambda))
sd_5N <- list(list(lambda = subset(mean_dens1, Rep==5 & Env=="N" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==5 & Env=="N" & SR==2)$sd_lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==5 & Env=="N" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==5 & Env=="N" & SR==5)$sd_lambda))
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==1),"Tu_lambda"]<-c(cxr_C.R1$fixed_terms[[1]]$lambda-sd_1N[[1]]$lambda,cxr_C.R1$fixed_terms[[2]]$lambda-sd_1N[[2]]$lambda)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==1),"Te_lambda"]<-c(cxr_C.R1$fixed_terms[[3]]$lambda-sd_1N[[3]]$lambda,cxr_C.R1$fixed_terms[[3]]$lambda-sd_1N[[3]]$lambda, cxr_C.R1$fixed_terms[[4]]$lambda-sd_1N[[4]]$lambda,cxr_C.R1$fixed_terms[[4]]$lambda-sd_1N[[4]]$lambda)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==2),"Tu_lambda"]<-c(cxr_C.R2_sr1$fixed_terms[[1]]-sd_2N[[1]]$lambda,cxr_C.R2_sr1$fixed_terms[[1]]-sd_2N[[1]]$lambda)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==2),"Te_lambda"]<-c(cxr_C.R2_sr4$fixed_terms[[1]]-sd_2N[[2]]$lambda,cxr_C.R2_sr5$fixed_terms[[1]]-sd_2N[[3]]$lambda)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==3),"Tu_lambda"]<-c(cxr_C.R3$fixed_terms[[1]]$lambda-sd_3N[[1]]$lambda,cxr_C.R3$fixed_terms[[2]]$lambda-sd_3N[[2]]$lambda)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==3),"Te_lambda"]<-c(cxr_C.R3$fixed_terms[[3]]$lambda-sd_3N[[3]]$lambda,cxr_C.R3$fixed_terms[[3]]$lambda-sd_3N[[3]]$lambda, cxr_C.R3$fixed_terms[[4]]$lambda-sd_3N[[4]]$lambda,cxr_C.R3$fixed_terms[[4]]$lambda-sd_3N[[4]]$lambda)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==4),"Tu_lambda"]<-c(cxr_C.R4$fixed_terms[[1]]$lambda-sd_4N[[1]]$lambda,cxr_C.R4$fixed_terms[[2]]$lambda-sd_4N[[2]]$lambda)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==4),"Te_lambda"]<-c(cxr_C.R4$fixed_terms[[3]]$lambda-sd_4N[[3]]$lambda,cxr_C.R4$fixed_terms[[3]]$lambda-sd_4N[[3]]$lambda, cxr_C.R4$fixed_terms[[4]]$lambda-sd_4N[[4]]$lambda,cxr_C.R4$fixed_terms[[4]]$lambda-sd_4N[[4]]$lambda)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==5),"Tu_lambda"]<-c(cxr_C.R5$fixed_terms[[1]]$lambda-sd_5N[[1]]$lambda,cxr_C.R5$fixed_terms[[2]]$lambda-sd_5N[[2]]$lambda)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==5),"Te_lambda"]<-c(cxr_C.R5$fixed_terms[[3]]$lambda-sd_5N[[3]]$lambda,cxr_C.R5$fixed_terms[[3]]$lambda-sd_5N[[3]]$lambda, cxr_C.R5$fixed_terms[[4]]$lambda-sd_5N[[4]]$lambda,cxr_C.R5$fixed_terms[[4]]$lambda-sd_5N[[4]]$lambda)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==1),"Tu_intra"]<-rep(c(cxr_C.R1_intra$alpha_matrix[1,1]-cxr_C.R1_intra$alpha_matrix_standard_error[1,1], cxr_C.R1_intra$alpha_matrix[2,1]-cxr_C.R1_intra$alpha_matrix_standard_error[2,1]), 2)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==1),"Te_intra"]<-rep(c(cxr_C.R1_intra$alpha_matrix[3,1]-cxr_C.R1_intra$alpha_matrix_standard_error[3,1], cxr_C.R1_intra$alpha_matrix[4,1]-cxr_C.R1_intra$alpha_matrix_standard_error[4,1]), each=2)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==2),"Tu_intra"]<-rep(c(cxr_C.R2_intra$alpha_matrix[1,1]-cxr_C.R2_intra$alpha_matrix_standard_error[1,1]), 2)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==2),"Te_intra"]<-c(cxr_C.R2_intra$alpha_matrix[2,1]-cxr_C.R2_intra$alpha_matrix_standard_error[2,1], cxr_C.R2_intra$alpha_matrix[3,1]-cxr_C.R2_intra$alpha_matrix_standard_error[3,1])
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==3),"Tu_intra"]<-rep(c(cxr_C.R3_intra$alpha_matrix[1,1]-cxr_C.R3_intra$alpha_matrix_standard_error[1,1], cxr_C.R3_intra$alpha_matrix[2,1]-cxr_C.R3_intra$alpha_matrix_standard_error[2,1]), 2)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==3),"Te_intra"]<-rep(c(cxr_C.R3_intra$alpha_matrix[3,1]-cxr_C.R3_intra$alpha_matrix_standard_error[3,1], cxr_C.R3_intra$alpha_matrix[4,1]-cxr_C.R3_intra$alpha_matrix_standard_error[4,1]), each=2)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==4),"Tu_intra"]<-rep(c(cxr_C.R4_intra$alpha_matrix[1,1]-cxr_C.R4_intra$alpha_matrix_standard_error[1,1], cxr_C.R4_intra$alpha_matrix[2,1]-cxr_C.R4_intra$alpha_matrix_standard_error[2,1]), 2)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==4),"Te_intra"]<-rep(c(cxr_C.R4_intra$alpha_matrix[3,1]-cxr_C.R4_intra$alpha_matrix_standard_error[3,1], cxr_C.R4_intra$alpha_matrix[4,1]-cxr_C.R4_intra$alpha_matrix_standard_error[4,1]), each=2)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==5),"Tu_intra"]<-rep(c(cxr_C.R5_intra$alpha_matrix[1,1]-cxr_C.R5_intra$alpha_matrix_standard_error[1,1], cxr_C.R5_intra$alpha_matrix[2,1]-cxr_C.R5_intra$alpha_matrix_standard_error[2,1]), 2)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==5),"Te_intra"]<-rep(c(cxr_C.R5_intra$alpha_matrix[3,1]-cxr_C.R5_intra$alpha_matrix_standard_error[3,1], cxr_C.R5_intra$alpha_matrix[4,1]-cxr_C.R5_intra$alpha_matrix_standard_error[4,1]), each=2)
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==1),"Tu_inter"]<-c(cxr_C.R1$alpha_matrix[1,3]-cxr_C.R1$alpha_matrix_standard_error[1,3], cxr_C.R1$alpha_matrix[2,3]-cxr_C.R1$alpha_matrix_standard_error[2,3],cxr_C.R1$alpha_matrix[1,4]-cxr_C.R1$alpha_matrix_standard_error[1,4], cxr_C.R1$alpha_matrix[2,4]-cxr_C.R1$alpha_matrix_standard_error[2,4])
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==1),"Te_inter"]<-c(cxr_C.R1$alpha_matrix[3,1]-cxr_C.R1$alpha_matrix_standard_error[3,1], cxr_C.R1$alpha_matrix[3,2]-cxr_C.R1$alpha_matrix_standard_error[3,2],cxr_C.R1$alpha_matrix[4,1]-cxr_C.R1$alpha_matrix_standard_error[4,1], cxr_C.R1$alpha_matrix[4,2]-cxr_C.R1$alpha_matrix_standard_error[4,2])
cxr_param_C_lower[which(cxr_param_C_lower$Replicate==2),"Tu_inter"]<-c(cxr_C.R2_sr1$alpha_inter[1]-cxr_C.R2_sr1$alpha_inter_standard_error[1], cxr_C.R2$alpha_inter[2]-cxr_C.R2_sr1$alpha_inter_standard_error[2])
Error: object 'cxr_C.R2' not found
cxr_param_CC<-expand.grid(Tu_Regime=c("SR1", "SR2"), Te_Regime=c("SR4", "SR5"), Replicate=c(1,2,3,4,5), Environment=c("Cd"))
cxr_param_CC$Tu_lambda<-0
cxr_param_CC$Te_lambda<-0
cxr_param_CC$Tu_intra<-0
cxr_param_CC$Te_intra<-0
cxr_param_CC$Tu_inter<-0
cxr_param_CC$Te_inter<-0
#removing SR2 for replicate 2
cxr_param_CC<-cxr_param_CC[-which(cxr_param_CC$Replicate==2 & cxr_param_CC$Tu_Regime=="SR2"),]
cxr_param_CC[which(cxr_param_CC$Replicate==1),"Tu_lambda"]<-c(cxr_C.R1_cd$fixed_terms[[1]]$lambda,cxr_C.R1_cd$fixed_terms[[2]]$lambda)
cxr_param_CC[which(cxr_param_CC$Replicate==1),"Te_lambda"]<-c(cxr_C.R1_cd$fixed_terms[[3]]$lambda,cxr_C.R1_cd$fixed_terms[[3]]$lambda, cxr_C.R1_cd$fixed_terms[[4]]$lambda,cxr_C.R1_cd$fixed_terms[[4]]$lambda)
cxr_param_CC[which(cxr_param_CC$Replicate==2),"Tu_lambda"]<-c(cxr_C.R2_cd_sr1$fixed_terms[[1]],cxr_C.R2_cd_sr1$fixed_terms[[1]])
cxr_param_CC[which(cxr_param_CC$Replicate==2),"Te_lambda"]<-c(cxr_C.R2_cd_sr4$fixed_terms[[1]], cxr_C.R2_cd_sr5$fixed_terms[[1]])
cxr_param_CC[which(cxr_param_CC$Replicate==3),"Tu_lambda"]<-c(cxr_C.R3_cd$fixed_terms[[1]]$lambda,cxr_C.R3_cd$fixed_terms[[2]]$lambda)
cxr_param_CC[which(cxr_param_CC$Replicate==3),"Te_lambda"]<-c(cxr_C.R3_cd$fixed_terms[[3]]$lambda,cxr_C.R3_cd$fixed_terms[[3]]$lambda, cxr_C.R3_cd$fixed_terms[[4]]$lambda,cxr_C.R3_cd$fixed_terms[[4]]$lambda)
cxr_param_CC[which(cxr_param_CC$Replicate==4),"Tu_lambda"]<-c(cxr_C.R4_cd$fixed_terms[[1]]$lambda,cxr_C.R4_cd$fixed_terms[[2]]$lambda)
cxr_param_CC[which(cxr_param_CC$Replicate==4),"Te_lambda"]<-c(cxr_C.R4_cd$fixed_terms[[3]]$lambda,cxr_C.R4_cd$fixed_terms[[3]]$lambda, cxr_C.R4_cd$fixed_terms[[4]]$lambda,cxr_C.R4_cd$fixed_terms[[4]]$lambda)
cxr_param_CC[which(cxr_param_CC$Replicate==5),"Tu_lambda"]<-c(cxr_C.R5_cd$fixed_terms[[1]]$lambda,cxr_C.R5_cd$fixed_terms[[2]]$lambda)
cxr_param_CC[which(cxr_param_CC$Replicate==5),"Te_lambda"]<-c(cxr_C.R5_cd$fixed_terms[[3]]$lambda,cxr_C.R5_cd$fixed_terms[[3]]$lambda, cxr_C.R5_cd$fixed_terms[[4]]$lambda,cxr_C.R5_cd$fixed_terms[[4]]$lambda)
cxr_param_CC[which(cxr_param_CC$Replicate==1),"Tu_intra"]<-rep(c(cxr_C.R1_cd_intra$alpha_matrix[1,1], cxr_C.R1_cd_intra$alpha_matrix[2,1]), 2)
cxr_param_CC[which(cxr_param_CC$Replicate==1),"Te_intra"]<-rep(c(cxr_C.R1_cd_intra$alpha_matrix[3,1], cxr_C.R1_cd_intra$alpha_matrix[4,1]), each=2)
cxr_param_CC[which(cxr_param_CC$Replicate==2),"Tu_intra"]<-rep(c(cxr_C.R2_cd_intra$alpha_matrix[1,1]), 2)
cxr_param_CC[which(cxr_param_CC$Replicate==2),"Te_intra"]<-c(cxr_C.R2_cd_intra$alpha_matrix[2,1], cxr_C.R2_cd_intra$alpha_matrix[3,1])
cxr_param_CC[which(cxr_param_CC$Replicate==3),"Tu_intra"]<-rep(c(cxr_C.R3_cd_intra$alpha_matrix[1,1], cxr_C.R3_cd_intra$alpha_matrix[2,1]), 2)
cxr_param_CC[which(cxr_param_CC$Replicate==3),"Te_intra"]<-rep(c(cxr_C.R3_cd_intra$alpha_matrix[3,1], cxr_C.R3_cd_intra$alpha_matrix[4,1]), each=2)
cxr_param_CC[which(cxr_param_CC$Replicate==4),"Tu_intra"]<-rep(c(cxr_C.R4_cd_intra$alpha_matrix[1,1], cxr_C.R4_cd_intra$alpha_matrix[2,1]), 2)
cxr_param_CC[which(cxr_param_CC$Replicate==4),"Te_intra"]<-rep(c(cxr_C.R4_cd_intra$alpha_matrix[3,1], cxr_C.R4_cd_intra$alpha_matrix[4,1]), each=2)
cxr_param_CC[which(cxr_param_CC$Replicate==5),"Tu_intra"]<-rep(c(cxr_C.R5_cd_intra$alpha_matrix[1,1], cxr_C.R5_cd_intra$alpha_matrix[2,1]), 2)
cxr_param_CC[which(cxr_param_CC$Replicate==5),"Te_intra"]<-rep(c(cxr_C.R5_cd_intra$alpha_matrix[3,1], cxr_C.R5_cd_intra$alpha_matrix[4,1]), each=2)
cxr_param_CC[which(cxr_param_CC$Replicate==1),"Tu_inter"]<-c(cxr_C.R1_cd$alpha_matrix[1,3], cxr_C.R1_cd$alpha_matrix[2,3],cxr_C.R1_cd$alpha_matrix[1,4], cxr_C.R1_cd$alpha_matrix[2,4])
cxr_param_CC[which(cxr_param_CC$Replicate==1),"Te_inter"]<-c(cxr_C.R1_cd$alpha_matrix[3,1], cxr_C.R1_cd$alpha_matrix[3,2],cxr_C.R1_cd$alpha_matrix[4,1], cxr_C.R1_cd$alpha_matrix[4,2])
cxr_param_CC[which(cxr_param_CC$Replicate==2),"Tu_inter"]<-c(cxr_C.R2_cd_sr1$alpha_inter[1], cxr_C.R2_cd_sr1$alpha_inter[2])
cxr_param_CC[which(cxr_param_CC$Replicate==2),"Te_inter"]<-c(cxr_C.R2_cd_sr4$alpha_inter[1], cxr_C.R2_cd_sr5$alpha_inter[1])
cxr_param_CC[which(cxr_param_CC$Replicate==3),"Tu_inter"]<-c(cxr_C.R3_cd$alpha_matrix[1,3], cxr_C.R3_cd$alpha_matrix[2,3],cxr_C.R3_cd$alpha_matrix[1,4], cxr_C.R3_cd$alpha_matrix[2,4])
cxr_param_CC[which(cxr_param_CC$Replicate==3),"Te_inter"]<-c(cxr_C.R3_cd$alpha_matrix[3,1], cxr_C.R3_cd$alpha_matrix[3,2],cxr_C.R3_cd$alpha_matrix[4,1], cxr_C.R3_cd$alpha_matrix[4,2])
cxr_param_CC[which(cxr_param_CC$Replicate==4),"Tu_inter"]<-c(cxr_C.R4_cd$alpha_matrix[1,3], cxr_C.R4_cd$alpha_matrix[2,3],cxr_C.R4_cd$alpha_matrix[1,4], cxr_C.R4_cd$alpha_matrix[2,4])
cxr_param_CC[which(cxr_param_CC$Replicate==4),"Te_inter"]<-c(cxr_C.R4_cd$alpha_matrix[3,1], cxr_C.R4_cd$alpha_matrix[3,2],cxr_C.R4_cd$alpha_matrix[4,1], cxr_C.R4_cd$alpha_matrix[4,2])
cxr_param_CC[which(cxr_param_CC$Replicate==5),"Tu_inter"]<-c(cxr_C.R5_cd$alpha_matrix[1,3], cxr_C.R5_cd$alpha_matrix[2,3],cxr_C.R5_cd$alpha_matrix[1,4], cxr_C.R5_cd$alpha_matrix[2,4])
cxr_param_CC[which(cxr_param_CC$Replicate==5),"Te_inter"]<-c(cxr_C.R5_cd$alpha_matrix[3,1], cxr_C.R5_cd$alpha_matrix[3,2],cxr_C.R5_cd$alpha_matrix[4,1], cxr_C.R5_cd$alpha_matrix[4,2])
### Lower
cxr_param_CC_lower<-expand.grid(Tu_Regime=c("SR1", "SR2"), Te_Regime=c("SR4", "SR5"), Replicate=c(1,2,3,4,5), Environment=c("Cd"))
cxr_param_CC_lower$Tu_lambda<-0
cxr_param_CC_lower$Te_lambda<-0
cxr_param_CC_lower$Tu_intra<-0
cxr_param_CC_lower$Te_intra<-0
cxr_param_CC_lower$Tu_inter<-0
cxr_param_CC_lower$Te_inter<-0
#removing SR2 for replicate 2
cxr_param_CC_lower<-cxr_param_CC_lower[-which(cxr_param_CC_lower$Replicate==2 & cxr_param_CC_lower$Tu_Regime=="SR2"),]
#Since the error comes directly from the data we need to create some lists with that information
sd_1C <- list(list(lambda = subset(mean_dens1, Rep==1 & Env=="Cd" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==1 & Env=="Cd" & SR==2)$sd_lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==1 & Env=="Cd" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==1 & Env=="Cd" & SR==5)$sd_lambda))
sd_2C <- list(list(lambda = subset(mean_dens1, Rep==2 & Env=="Cd" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==2 & Env=="Cd" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==2 & Env=="Cd" & SR==5)$sd_lambda))
sd_3C <- list(list(lambda = subset(mean_dens1, Rep==3 & Env=="Cd" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==3 & Env=="Cd" & SR==2)$sd_lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==3 & Env=="Cd" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==3 & Env=="Cd" & SR==5)$sd_lambda))
sd_4C <- list(list(lambda = subset(mean_dens1, Rep==4 & Env=="Cd" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==4 & Env=="Cd" & SR==2)$sd_lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==4 & Env=="Cd" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==4 & Env=="Cd" & SR==5)$sd_lambda))
sd_5C <- list(list(lambda = subset(mean_dens1, Rep==5 & Env=="Cd" & SR==1)$sd_lambda ), # focal sp 1
list(lambda = subset(mean_dens1, Rep==5 & Env=="Cd" & SR==2)$sd_lambda), # focal sp 2
list(lambda = subset(mean_dens1, Rep==5 & Env=="Cd" & SR==4)$sd_lambda),
list(lambda= subset(mean_dens1, Rep==5 & Env=="Cd" & SR==5)$sd_lambda))
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==1),"Tu_lambda"]<-c(cxr_C.R1_cd$fixed_terms[[1]]$lambda-sd_1C[[1]]$lambda,cxr_C.R1_cd$fixed_terms[[2]]$lambda-sd_1C[[2]]$lambda)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==1),"Te_lambda"]<-c(cxr_C.R1_cd$fixed_terms[[3]]$lambda-sd_1C[[3]]$lambda,cxr_C.R1_cd$fixed_terms[[3]]$lambda-sd_1C[[3]]$lambda, cxr_C.R1_cd$fixed_terms[[4]]$lambda-sd_1C[[4]]$lambda,cxr_C.R1_cd$fixed_terms[[4]]$lambda-sd_1C[[4]]$lambda)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==2),"Tu_lambda"]<-c(cxr_C.R2_cd_sr1$fixed_terms$lambda-sd_2C[[1]]$lambda,cxr_C.R2_cd_sr1$fixed_terms$lambda-sd_2C[[1]]$lambda)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==2),"Te_lambda"]<-c(cxr_C.R2_cd_sr4$fixed_terms$lambda-sd_2C[[2]]$lambda,cxr_C.R2_cd_sr5$fixed_terms$lambda-sd_2C[[3]]$lambda)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==3),"Tu_lambda"]<-c(cxr_C.R3_cd$fixed_terms[[1]]$lambda-sd_3C[[1]]$lambda,cxr_C.R3_cd$fixed_terms[[2]]$lambda-sd_3C[[2]]$lambda)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==3),"Te_lambda"]<-c(cxr_C.R3_cd$fixed_terms[[3]]$lambda-sd_3C[[3]]$lambda,cxr_C.R3_cd$fixed_terms[[3]]$lambda-sd_3C[[3]]$lambda, cxr_C.R3_cd$fixed_terms[[4]]$lambda-sd_3C[[4]]$lambda,cxr_C.R3_cd$fixed_terms[[4]]$lambda-sd_3C[[4]]$lambda)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==4),"Tu_lambda"]<-c(cxr_C.R4_cd$fixed_terms[[1]]$lambda-sd_4C[[1]]$lambda,cxr_C.R4_cd$fixed_terms[[2]]$lambda-sd_4C[[2]]$lambda)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==4),"Te_lambda"]<-c(cxr_C.R4_cd$fixed_terms[[3]]$lambda-sd_4C[[3]]$lambda,cxr_C.R4_cd$fixed_terms[[3]]$lambda-sd_4C[[3]]$lambda, cxr_C.R4_cd$fixed_terms[[4]]$lambda-sd_4C[[4]]$lambda,cxr_C.R4_cd$fixed_terms[[4]]$lambda-sd_4C[[4]]$lambda)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==5),"Tu_lambda"]<-c(cxr_C.R5_cd$fixed_terms[[1]]$lambda-sd_5C[[1]]$lambda,cxr_C.R5_cd$fixed_terms[[2]]$lambda-sd_5C[[2]]$lambda)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==5),"Te_lambda"]<-c(cxr_C.R5_cd$fixed_terms[[3]]$lambda-sd_5C[[3]]$lambda,cxr_C.R5_cd$fixed_terms[[3]]$lambda-sd_5C[[3]]$lambda, cxr_C.R5_cd$fixed_terms[[4]]$lambda-sd_5C[[4]]$lambda,cxr_C.R5_cd$fixed_terms[[4]]$lambda-sd_5C[[4]]$lambda)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==1),"Tu_intra"]<-rep(c(cxr_C.R1_cd_intra$alpha_matrix[1,1]-cxr_C.R1_cd_intra$alpha_matrix_standard_error[1,1], cxr_C.R1_cd_intra$alpha_matrix[2,1]-cxr_C.R1_cd_intra$alpha_matrix_standard_error[2,1]), 2)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==1),"Te_intra"]<-rep(c(cxr_C.R1_cd_intra$alpha_matrix[3,1]-cxr_C.R1_cd_intra$alpha_matrix_standard_error[3,1], cxr_C.R1_cd_intra$alpha_matrix[4,1]-cxr_C.R1_cd_intra$alpha_matrix_standard_error[4,1]), each=2)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==2),"Tu_intra"]<-cxr_C.R2_cd_intra$alpha_matrix[1,1]-cxr_C.R2_cd_intra$alpha_matrix_standard_error[1,1]
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==2),"Te_intra"]<-c(cxr_C.R2_cd_intra$alpha_matrix[2,1]-cxr_C.R2_cd_intra$alpha_matrix_standard_error[2,1], cxr_C.R2_cd_intra$alpha_matrix[3,1]-cxr_C.R2_cd_intra$alpha_matrix_standard_error[3,1])
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==3),"Tu_intra"]<-rep(c(cxr_C.R3_cd_intra$alpha_matrix[1,1]-cxr_C.R3_cd_intra$alpha_matrix_standard_error[1,1], cxr_C.R3_cd_intra$alpha_matrix[2,1]-cxr_C.R3_cd_intra$alpha_matrix_standard_error[2,1]), 2)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==3),"Te_intra"]<-rep(c(cxr_C.R3_cd_intra$alpha_matrix[3,1]-cxr_C.R3_cd_intra$alpha_matrix_standard_error[3,1], cxr_C.R3_cd_intra$alpha_matrix[4,1]-cxr_C.R3_cd_intra$alpha_matrix_standard_error[4,1]), each=2)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==4),"Tu_intra"]<-rep(c(cxr_C.R4_cd_intra$alpha_matrix[1,1]-cxr_C.R4_cd_intra$alpha_matrix_standard_error[1,1], cxr_C.R4_cd_intra$alpha_matrix[2,1]-cxr_C.R4_cd_intra$alpha_matrix_standard_error[2,1]), 2)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==4),"Te_intra"]<-rep(c(cxr_C.R4_cd_intra$alpha_matrix[3,1]-cxr_C.R4_cd_intra$alpha_matrix_standard_error[3,1], cxr_C.R4_cd_intra$alpha_matrix[4,1]-cxr_C.R4_cd_intra$alpha_matrix_standard_error[4,1]), each=2)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==5),"Tu_intra"]<-rep(c(cxr_C.R5_cd_intra$alpha_matrix[1,1]-cxr_C.R5_cd_intra$alpha_matrix_standard_error[1,1], cxr_C.R5_cd_intra$alpha_matrix[2,1]-cxr_C.R5_cd_intra$alpha_matrix_standard_error[2,1]), 2)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==5),"Te_intra"]<-rep(c(cxr_C.R5_cd_intra$alpha_matrix[3,1]-cxr_C.R5_cd_intra$alpha_matrix_standard_error[3,1], cxr_C.R5_cd_intra$alpha_matrix[4,1]-cxr_C.R5_cd_intra$alpha_matrix_standard_error[4,1]), each=2)
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==1),"Tu_inter"]<-c(cxr_C.R1_cd$alpha_matrix[1,3]-cxr_C.R1_cd$alpha_matrix_standard_error[1,3], cxr_C.R1_cd$alpha_matrix[2,3]-cxr_C.R1_cd$alpha_matrix_standard_error[2,3],cxr_C.R1_cd$alpha_matrix[1,4]-cxr_C.R1_cd$alpha_matrix_standard_error[1,4], cxr_C.R1_cd$alpha_matrix[2,4]-cxr_C.R1_cd$alpha_matrix_standard_error[2,4])
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==1),"Te_inter"]<-c(cxr_C.R1_cd$alpha_matrix[3,1]-cxr_C.R1_cd$alpha_matrix_standard_error[3,1], cxr_C.R1_cd$alpha_matrix[3,2]-cxr_C.R1_cd$alpha_matrix_standard_error[3,2],cxr_C.R1_cd$alpha_matrix[4,1]-cxr_C.R1_cd$alpha_matrix_standard_error[4,1], cxr_C.R1_cd$alpha_matrix[4,2]-cxr_C.R1_cd$alpha_matrix_standard_error[4,2])
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==2),"Tu_inter"]<-c(cxr_C.R2_cd_sr1$alpha_inter[1]-cxr_C.R2_cd_sr1$alpha_inter_standard_error[1], cxr_C.R2_cd_sr1$alpha_inter[2]-cxr_C.R2_cd_sr1$alpha_inter_standard_error[2])
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==2),"Te_inter"]<-c(cxr_C.R2_cd_sr4$alpha_inter[1]-cxr_C.R2_cd_sr4$alpha_inter_standard_error[1], cxr_C.R2_cd_sr5$alpha_inter[1]-cxr_C.R2_cd_sr5$alpha_inter_standard_error[1])
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==3),"Tu_inter"]<-c(cxr_C.R3_cd$alpha_matrix[1,3]-cxr_C.R3_cd$alpha_matrix_standard_error[1,3], cxr_C.R3_cd$alpha_matrix[2,3]-cxr_C.R3_cd$alpha_matrix_standard_error[2,3],cxr_C.R3_cd$alpha_matrix[1,4]-cxr_C.R3_cd$alpha_matrix_standard_error[1,4], cxr_C.R3_cd$alpha_matrix[2,4]-cxr_C.R3_cd$alpha_matrix_standard_error[2,4])
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==3),"Te_inter"]<-c(cxr_C.R3_cd$alpha_matrix[3,1]-cxr_C.R3_cd$alpha_matrix_standard_error[3,1], cxr_C.R3_cd$alpha_matrix[3,2]-cxr_C.R3_cd$alpha_matrix_standard_error[3,2],cxr_C.R3_cd$alpha_matrix[4,1]-cxr_C.R3_cd$alpha_matrix_standard_error[4,1], cxr_C.R3_cd$alpha_matrix[4,2]-cxr_C.R3_cd$alpha_matrix_standard_error[4,2])
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==4),"Tu_inter"]<-c(cxr_C.R4_cd$alpha_matrix[1,3]-cxr_C.R4_cd$alpha_matrix_standard_error[1,3], cxr_C.R4_cd$alpha_matrix[2,3]-cxr_C.R4_cd$alpha_matrix_standard_error[2,3],cxr_C.R4_cd$alpha_matrix[1,4]-cxr_C.R4_cd$alpha_matrix_standard_error[1,4], cxr_C.R4_cd$alpha_matrix[2,4]-cxr_C.R4_cd$alpha_matrix_standard_error[2,4])
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==4),"Te_inter"]<-c(cxr_C.R4_cd$alpha_matrix[3,1]-cxr_C.R4_cd$alpha_matrix_standard_error[3,1], cxr_C.R4_cd$alpha_matrix[3,2]-cxr_C.R4_cd$alpha_matrix_standard_error[3,2],cxr_C.R4_cd$alpha_matrix[4,1]-cxr_C.R4_cd$alpha_matrix_standard_error[4,1], cxr_C.R4_cd$alpha_matrix[4,2]-cxr_C.R4_cd$alpha_matrix_standard_error[4,2])
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==5),"Tu_inter"]<-c(cxr_C.R5_cd$alpha_matrix[1,3]-cxr_C.R5_cd$alpha_matrix_standard_error[1,3], cxr_C.R5_cd$alpha_matrix[2,3]-cxr_C.R5_cd$alpha_matrix_standard_error[2,3],cxr_C.R5_cd$alpha_matrix[1,4]-cxr_C.R5_cd$alpha_matrix_standard_error[1,4], cxr_C.R5_cd$alpha_matrix[2,4]-cxr_C.R5_cd$alpha_matrix_standard_error[2,4])
cxr_param_CC_lower[which(cxr_param_CC_lower$Replicate==5),"Te_inter"]<-c(cxr_C.R5_cd$alpha_matrix[3,1]-cxr_C.R5_cd$alpha_matrix_standard_error[3,1], cxr_C.R5_cd$alpha_matrix[3,2]-cxr_C.R5_cd$alpha_matrix_standard_error[3,2],cxr_C.R5_cd$alpha_matrix[4,1]-cxr_C.R5_cd$alpha_matrix_standard_error[4,1], cxr_C.R5_cd$alpha_matrix[4,2]-cxr_C.R5_cd$alpha_matrix_standard_error[4,2])
### upper
cxr_param_CC_upper<-expand.grid(Tu_Regime=c("SR1", "SR2"), Te_Regime=c("SR4", "SR5"), Replicate=c(1,2,3,4,5), Environment=c("Cd"))
cxr_param_CC_upper$Tu_lambda<-0
cxr_param_CC_upper$Te_lambda<-0
cxr_param_CC_upper$Tu_intra<-0
cxr_param_CC_upper$Te_intra<-0
cxr_param_CC_upper$Tu_inter<-0
cxr_param_CC_upper$Te_inter<-0
#removing SR2 for replicate 2
cxr_param_CC_upper<-cxr_param_CC_upper[-which(cxr_param_CC_upper$Replicate==2 & cxr_param_CC_upper$Tu_Regime=="SR2"),]
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==1),"Tu_lambda"]<-c(cxr_C.R1_cd$fixed_terms[[1]]$lambda+sd_1C[[1]]$lambda,cxr_C.R1_cd$fixed_terms[[2]]$lambda+sd_1C[[2]]$lambda)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==1),"Te_lambda"]<-c(cxr_C.R1_cd$fixed_terms[[3]]$lambda+sd_1C[[3]]$lambda,cxr_C.R1_cd$fixed_terms[[3]]$lambda+sd_1C[[3]]$lambda, cxr_C.R1_cd$fixed_terms[[4]]$lambda+sd_1C[[4]]$lambda,cxr_C.R1_cd$fixed_terms[[4]]$lambda+sd_1C[[4]]$lambda)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==2),"Tu_lambda"]<-c(cxr_C.R2_cd_sr1$fixed_terms$lambda+sd_2C[[1]]$lambda,cxr_C.R2_cd_sr1$fixed_terms$lambda+sd_2C[[1]]$lambda)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==2),"Te_lambda"]<-c(cxr_C.R2_cd_sr4$fixed_terms$lambda+sd_2C[[2]]$lambda,cxr_C.R2_cd_sr5$fixed_terms$lambda+sd_2C[[3]]$lambda)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==3),"Tu_lambda"]<-c(cxr_C.R3_cd$fixed_terms[[1]]$lambda+sd_3C[[1]]$lambda,cxr_C.R3_cd$fixed_terms[[2]]$lambda+sd_3C[[2]]$lambda)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==3),"Te_lambda"]<-c(cxr_C.R3_cd$fixed_terms[[3]]$lambda+sd_3C[[3]]$lambda,cxr_C.R3_cd$fixed_terms[[3]]$lambda+sd_3C[[3]]$lambda, cxr_C.R3_cd$fixed_terms[[4]]$lambda+sd_3C[[4]]$lambda,cxr_C.R3_cd$fixed_terms[[4]]$lambda+sd_3C[[4]]$lambda)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==4),"Tu_lambda"]<-c(cxr_C.R4_cd$fixed_terms[[1]]$lambda+sd_4C[[1]]$lambda,cxr_C.R4_cd$fixed_terms[[2]]$lambda+sd_4C[[2]]$lambda)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==4),"Te_lambda"]<-c(cxr_C.R4_cd$fixed_terms[[3]]$lambda+sd_4C[[3]]$lambda,cxr_C.R4_cd$fixed_terms[[3]]$lambda+sd_4C[[3]]$lambda, cxr_C.R4_cd$fixed_terms[[4]]$lambda+sd_4C[[4]]$lambda,cxr_C.R4_cd$fixed_terms[[4]]$lambda+sd_4C[[4]]$lambda)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==5),"Tu_lambda"]<-c(cxr_C.R5_cd$fixed_terms[[1]]$lambda+sd_5C[[1]]$lambda,cxr_C.R5_cd$fixed_terms[[2]]$lambda+sd_5C[[2]]$lambda)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==5),"Te_lambda"]<-c(cxr_C.R5_cd$fixed_terms[[3]]$lambda+sd_5C[[3]]$lambda,cxr_C.R5_cd$fixed_terms[[3]]$lambda+sd_5C[[3]]$lambda, cxr_C.R5_cd$fixed_terms[[4]]$lambda+sd_5C[[4]]$lambda,cxr_C.R5_cd$fixed_terms[[4]]$lambda+sd_5C[[4]]$lambda)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==1),"Tu_intra"]<-rep(c(cxr_C.R1_cd_intra$alpha_matrix[1,1]+cxr_C.R1_cd_intra$alpha_matrix_standard_error[1,1], cxr_C.R1_cd_intra$alpha_matrix[2,1]+cxr_C.R1_cd_intra$alpha_matrix_standard_error[2,1]), 2)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==1),"Te_intra"]<-rep(c(cxr_C.R1_cd_intra$alpha_matrix[3,1]+cxr_C.R1_cd_intra$alpha_matrix_standard_error[3,1], cxr_C.R1_cd_intra$alpha_matrix[4,1]+cxr_C.R1_cd_intra$alpha_matrix_standard_error[4,1]), each=2)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==2),"Tu_intra"]<-c(cxr_C.R2_cd_intra$alpha_matrix[1,1]+cxr_C.R2_cd_intra$alpha_matrix_standard_error[1,1],cxr_C.R2_cd_intra$alpha_matrix[1,1]+cxr_C.R2_cd_intra$alpha_matrix_standard_error[1,1])
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==2),"Te_intra"]<-c(cxr_C.R2_cd_intra$alpha_matrix[2,1]+cxr_C.R2_cd_intra$alpha_matrix_standard_error[2,1],cxr_C.R2_cd_intra$alpha_matrix[3,1]+cxr_C.R2_cd_intra$alpha_matrix_standard_error[3,1])
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==3),"Tu_intra"]<-rep(c(cxr_C.R3_cd_intra$alpha_matrix[1,1]+cxr_C.R3_cd_intra$alpha_matrix_standard_error[1,1], cxr_C.R3_cd_intra$alpha_matrix[2,1]+cxr_C.R3_cd_intra$alpha_matrix_standard_error[2,1]), 2)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==3),"Te_intra"]<-rep(c(cxr_C.R3_cd_intra$alpha_matrix[3,1]+cxr_C.R3_cd_intra$alpha_matrix_standard_error[3,1], cxr_C.R3_cd_intra$alpha_matrix[4,1]+cxr_C.R3_cd_intra$alpha_matrix_standard_error[4,1]), each=2)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==4),"Tu_intra"]<-rep(c(cxr_C.R4_cd_intra$alpha_matrix[1,1]+cxr_C.R4_cd_intra$alpha_matrix_standard_error[1,1], cxr_C.R4_cd_intra$alpha_matrix[2,1]+cxr_C.R4_cd_intra$alpha_matrix_standard_error[2,1]), 2)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==4),"Te_intra"]<-rep(c(cxr_C.R4_cd_intra$alpha_matrix[3,1]+cxr_C.R4_cd_intra$alpha_matrix_standard_error[3,1], cxr_C.R4_cd_intra$alpha_matrix[4,1]+cxr_C.R4_cd_intra$alpha_matrix_standard_error[4,1]), each=2)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==5),"Tu_intra"]<-rep(c(cxr_C.R5_cd_intra$alpha_matrix[1,1]+cxr_C.R5_cd_intra$alpha_matrix_standard_error[1,1], cxr_C.R5_cd_intra$alpha_matrix[2,1]+cxr_C.R5_cd_intra$alpha_matrix_standard_error[2,1]), 2)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==5),"Te_intra"]<-rep(c(cxr_C.R5_cd_intra$alpha_matrix[3,1]+cxr_C.R5_cd_intra$alpha_matrix_standard_error[3,1], cxr_C.R5_cd_intra$alpha_matrix[4,1]+cxr_C.R5_cd_intra$alpha_matrix_standard_error[4,1]), each=2)
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==1),"Tu_inter"]<-c(cxr_C.R1_cd$alpha_matrix[1,3]+cxr_C.R1_cd$alpha_matrix_standard_error[1,3], cxr_C.R1_cd$alpha_matrix[2,3]+cxr_C.R1_cd$alpha_matrix_standard_error[2,3],cxr_C.R1_cd$alpha_matrix[1,4]+cxr_C.R1_cd$alpha_matrix_standard_error[1,4], cxr_C.R1_cd$alpha_matrix[2,4]+cxr_C.R1_cd$alpha_matrix_standard_error[2,4])
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==1),"Te_inter"]<-c(cxr_C.R1_cd$alpha_matrix[3,1]+cxr_C.R1_cd$alpha_matrix_standard_error[3,1], cxr_C.R1_cd$alpha_matrix[3,2]+cxr_C.R1_cd$alpha_matrix_standard_error[3,2],cxr_C.R1_cd$alpha_matrix[4,1]+cxr_C.R1_cd$alpha_matrix_standard_error[4,1], cxr_C.R1_cd$alpha_matrix[4,2]+cxr_C.R1_cd$alpha_matrix_standard_error[4,2])
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==2),"Tu_inter"]<-c(cxr_C.R2_cd_sr1$alpha_inter[1]+cxr_C.R2_cd_sr1$alpha_inter_standard_error[1], cxr_C.R2_cd_sr1$alpha_inter[2]+cxr_C.R2_cd_sr1$alpha_inter_standard_error[2])
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==2),"Te_inter"]<-c(cxr_C.R2_cd_sr4$alpha_inter[1]+cxr_C.R2_cd_sr4$alpha_inter_standard_error[1], cxr_C.R2_cd_sr5$alpha_inter[1]+cxr_C.R2_cd_sr5$alpha_inter_standard_error[1])
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==3),"Tu_inter"]<-c(cxr_C.R3_cd$alpha_matrix[1,3]+cxr_C.R3_cd$alpha_matrix_standard_error[1,3], cxr_C.R3_cd$alpha_matrix[2,3]+cxr_C.R3_cd$alpha_matrix_standard_error[2,3],cxr_C.R3_cd$alpha_matrix[1,4]+cxr_C.R3_cd$alpha_matrix_standard_error[1,4], cxr_C.R3_cd$alpha_matrix[2,4]+cxr_C.R3_cd$alpha_matrix_standard_error[2,4])
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==3),"Te_inter"]<-c(cxr_C.R3_cd$alpha_matrix[3,1]+cxr_C.R3_cd$alpha_matrix_standard_error[3,1], cxr_C.R3_cd$alpha_matrix[3,2]+cxr_C.R3_cd$alpha_matrix_standard_error[3,2],cxr_C.R3_cd$alpha_matrix[4,1]+cxr_C.R3_cd$alpha_matrix_standard_error[4,1], cxr_C.R3_cd$alpha_matrix[4,2]+cxr_C.R3_cd$alpha_matrix_standard_error[4,2])
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==4),"Tu_inter"]<-c(cxr_C.R4_cd$alpha_matrix[1,3]+cxr_C.R4_cd$alpha_matrix_standard_error[1,3], cxr_C.R4_cd$alpha_matrix[2,3]+cxr_C.R4_cd$alpha_matrix_standard_error[2,3],cxr_C.R4_cd$alpha_matrix[1,4]+cxr_C.R4_cd$alpha_matrix_standard_error[1,4], cxr_C.R4_cd$alpha_matrix[2,4]+cxr_C.R4_cd$alpha_matrix_standard_error[2,4])
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==4),"Te_inter"]<-c(cxr_C.R4_cd$alpha_matrix[3,1]+cxr_C.R4_cd$alpha_matrix_standard_error[3,1], cxr_C.R4_cd$alpha_matrix[3,2]+cxr_C.R4_cd$alpha_matrix_standard_error[3,2],cxr_C.R4_cd$alpha_matrix[4,1]+cxr_C.R4_cd$alpha_matrix_standard_error[4,1], cxr_C.R4_cd$alpha_matrix[4,2]+cxr_C.R4_cd$alpha_matrix_standard_error[4,2])
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==5),"Tu_inter"]<-c(cxr_C.R5_cd$alpha_matrix[1,3]+cxr_C.R5_cd$alpha_matrix_standard_error[1,3], cxr_C.R5_cd$alpha_matrix[2,3]+cxr_C.R5_cd$alpha_matrix_standard_error[2,3],cxr_C.R5_cd$alpha_matrix[1,4]+cxr_C.R5_cd$alpha_matrix_standard_error[1,4], cxr_C.R5_cd$alpha_matrix[2,4]+cxr_C.R5_cd$alpha_matrix_standard_error[2,4])
cxr_param_CC_upper[which(cxr_param_CC_upper$Replicate==5),"Te_inter"]<-c(cxr_C.R5_cd$alpha_matrix[3,1]+cxr_C.R5_cd$alpha_matrix_standard_error[3,1], cxr_C.R5_cd$alpha_matrix[3,2]+cxr_C.R5_cd$alpha_matrix_standard_error[3,2],cxr_C.R5_cd$alpha_matrix[4,1]+cxr_C.R5_cd$alpha_matrix_standard_error[4,1], cxr_C.R5_cd$alpha_matrix[4,2]+cxr_C.R5_cd$alpha_matrix_standard_error[4,2])
param_all_C<-as.data.frame(rbind(cxr_param_C, cxr_param_CC))
param_all_C_lower<-as.data.frame(rbind(cxr_param_C_lower, cxr_param_CC_lower))
param_all_C_upper<-as.data.frame(rbind(cxr_param_C_upper, cxr_param_CC_upper))
param_all_C_lower
param_all_C_upper
write.csv(param_all_C, "./NewAnalyses/cxr_lambda_fixed_nested/parameters_cxr_lambda_fixed.csv")
write.csv(param_all_C_upper, "./NewAnalyses/cxr_lambda_fixed_nested/parameters_cxr_lambda_fixed_upper.csv")
write.csv(param_all_C_lower, "./NewAnalyses/cxr_lambda_fixed_nested/parameters_cxr_lambda_fixed_lower.csv")
param_all_C<-read.csv("./NewAnalyses/cxr_lambda_fixed_nested/parameters_cxr_lambda_fixed.csv")
param_all_C_upper<-read.csv("./NewAnalyses/cxr_lambda_fixed_nested/parameters_cxr_lambda_fixed_upper.csv")
param_all_C_lower<-read.csv("./NewAnalyses/cxr_lambda_fixed_nested/parameters_cxr_lambda_fixed_lower.csv")
param_all_C<-param_all_C[,-1]
param_all_C_upper<-param_all_C_upper[,-1]
param_all_C_lower<-param_all_C_lower[,-1]
param_all_C_long<-gather(param_all_C, parameter, value,Tu_lambda:Te_inter )
param_all_C_long$category<-mapvalues(param_all_C_long$parameter, c("Tu_lambda", "Te_lambda", "Tu_intra", "Te_intra","Tu_inter", "Te_inter"), c("lambda", "lambda", "intra", "intra", "inter", "inter"))
param_all_C_lower_long<-gather(param_all_C_lower, parameter, value,Tu_lambda:Te_inter )
param_all_C_lower_long$category<-mapvalues(param_all_C_lower_long$parameter, c("Tu_lambda", "Te_lambda", "Tu_intra", "Te_intra","Tu_inter", "Te_inter"), c("lambda", "lambda", "intra", "intra", "inter", "inter"))
param_all_C_upper_long<-gather(param_all_C_upper, parameter, value,Tu_lambda:Te_inter )
param_all_C_upper_long$category<-mapvalues(param_all_C_upper_long$parameter, c("Tu_lambda", "Te_lambda", "Tu_intra", "Te_intra","Tu_inter", "Te_inter"), c("lambda", "lambda", "intra", "intra", "inter", "inter"))
colnames(param_all_C_lower_long)[6]<-"lower"
colnames(param_all_C_upper_long)[6]<-"upper"
str(param_all_C_long)
'data.frame': 216 obs. of 7 variables:
$ Tu_Regime : chr "SR1" "SR2" "SR1" "SR2" ...
$ Te_Regime : chr "SR4" "SR4" "SR5" "SR5" ...
$ Replicate : int 1 1 1 1 2 2 3 3 3 3 ...
$ Environment: chr "N" "N" "N" "N" ...
$ parameter : chr "Tu_lambda" "Tu_lambda" "Tu_lambda" "Tu_lambda" ...
$ value : num 3.9 3.7 3.9 3.7 4.2 ...
$ category : chr "lambda" "lambda" "lambda" "lambda" ...
param_all_C_long<-cbind(param_all_C_long[,1:7],param_all_C_lower_long$lower, param_all_C_upper_long$upper)
colnames(param_all_C_long)[8:9]<-c("lower","upper")
# Comparison control vs evolved per environment
ggplot(subset(param_all_C_long, parameter=="Te_intra" & Tu_Regime=="SR1"), aes(x=Environment, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_nested/param_RK_intraTe_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_C_long, parameter=="Tu_intra" & Te_Regime=="SR4"), aes(fill=Tu_Regime, y=value, x=Environment))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_nested/param_RK_intraTu_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_C_long, parameter=="Te_inter"), aes(x=Environment, y=value, fill=Te_Regime))+
facet_grid(Tu_Regime~., labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. evansi)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_nested/param_RK_interTe_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_C_long, parameter=="Tu_inter"), aes(fill=Tu_Regime, y=value, x=Environment))+
facet_grid(Te_Regime~., labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot(outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.75))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. urticae)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_nested/param_RK_interTu_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_C_long, parameter=="Te_lambda" & Tu_Regime=="SR1"), aes(x=Environment, y=value, fill=Te_Regime))+
geom_boxplot( position=position_dodge2(0.5), outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. evansi)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_nested/param_RK_lambdaTe_w0_rep_env.pdf", width=20, height=15)
ggplot(subset(param_all_C_long, parameter=="Tu_lambda" & Te_Regime=="SR4"), aes(fill=Tu_Regime, y=value, x=Environment))+
geom_boxplot(position=position_dodge2(0.5), outlier.alpha =0.01)+
geom_point( position=position_dodge2(0.5))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. urticae)")+
xlab("Environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_nested/param_RK_lambdaTu_w0_rep_env.pdf", width=20, height=15)
#####################################################
density_aux<-seq(0, 10, by=(10/100))
pred_df_cxr_C<-as.data.frame(expand_grid(Density=density_aux, Tu_Regime=c("SR1","SR2"), Te_Regime=c("SR4","SR5"), Replicate=c(1:5), Environment=c("N", "Cd")))
pred_df_cxr_C$Tu_mean_intra<-sapply(c(1:length(pred_df_cxr_C[,1])), function(x){
alpha_i<-subset(param_all_C, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_C, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_C, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr_C$Density[x])
pred
})
pred_df_cxr_C$Tu_mean_inter<-sapply(c(1:length(pred_df_cxr_C[,1])), function(x){
alpha_i<-subset(param_all_C, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_C, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_C, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr_C$Density[x])
pred
})
pred_df_cxr_C$Tu_intra_L<-sapply(c(1:length(pred_df_cxr_C[,1])), function(x){
alpha_i<-subset(param_all_C_lower, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_C_lower, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_C_lower, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr_C$Density[x])
pred
})
pred_df_cxr_C$Tu_inter_L<-sapply(c(1:length(pred_df_cxr_C[,1])), function(x){
alpha_i<-subset(param_all_C_lower, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_C_lower, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_C_lower, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr_C$Density[x])
pred
})
pred_df_cxr_C$Tu_intra_U<-sapply(c(1:length(pred_df_cxr_C[,1])), function(x){
alpha_i<-subset(param_all_C_upper, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_C_upper, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_C_upper, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr_C$Density[x])
pred
})
pred_df_cxr_C$Tu_inter_U<-sapply(c(1:length(pred_df_cxr_C[,1])), function(x){
alpha_i<-subset(param_all_C_upper, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(param_all_C_upper, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_inter[1]
lambda<-subset(param_all_C_upper, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr_C$Density[x])
pred
})
pred_df_cxr_C$Te_mean_intra<-sapply(c(1:length(pred_df_cxr_C[,1])), function(x){
alpha_i<-subset(param_all_C, Environment==pred_df_cxr_C$Environment[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_C, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_C, Environment==pred_df_cxr_C$Environment[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr_C$Density[x])
pred
})
pred_df_cxr_C$Te_mean_inter<-sapply(c(1:length(pred_df_cxr_C[,1])), function(x){
alpha_i<-subset(param_all_C, Environment==pred_df_cxr_C$Environment[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_C, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_C, Environment==pred_df_cxr_C$Environment[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr_C$Density[x])
pred
})
pred_df_cxr_C$Te_intra_L<-sapply(c(1:length(pred_df_cxr_C[,1])), function(x){
alpha_i<-subset(param_all_C_lower, Environment==pred_df_cxr_C$Environment[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_C_lower, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_C_lower, Environment==pred_df_cxr_C$Environment[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr_C$Density[x])
pred
})
pred_df_cxr_C$Te_inter_L<-sapply(c(1:length(pred_df_cxr_C[,1])), function(x){
alpha_i<-subset(param_all_C_lower, Environment==pred_df_cxr_C$Environment[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_C_lower, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_C_lower, Environment==pred_df_cxr_C$Environment[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr_C$Density[x])
pred
})
pred_df_cxr_C$Te_intra_U<-sapply(c(1:length(pred_df_cxr_C[,1])), function(x){
alpha_i<-subset(param_all_C_upper, Environment==pred_df_cxr_C$Environment[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_C_upper, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_C_upper, Environment==pred_df_cxr_C$Environment[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_cxr_C$Density[x])
pred
})
pred_df_cxr_C$Te_inter_U<-sapply(c(1:length(pred_df_cxr_C[,1])), function(x){
alpha_i<-subset(param_all_C_upper, Environment==pred_df_cxr_C$Environment[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_intra[1]
alpha_ij<-subset(param_all_C_upper, Environment==pred_df_cxr_C$Environment[x] & Tu_Regime==pred_df_cxr_C$Tu_Regime[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_inter[1]
lambda<-subset(param_all_C_upper, Environment==pred_df_cxr_C$Environment[x] & Te_Regime==pred_df_cxr_C$Te_Regime[x] & Replicate==pred_df_cxr_C$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_cxr_C$Density[x])
pred
})
# Removing Tu evolved replicate 2 because there is no data
pred_df_cxr_C<-pred_df_cxr_C[-which(pred_df_cxr_C$Tu_Regime=="SR2" & pred_df_cxr_C$Replicate==2),]
# Transforming everything bellow 0 into 0 for the lower interval
pred_df_cxr_C$Te_inter_L[which(pred_df_cxr_C$Te_inter_L<0)]<-0
pred_df_cxr_C$Te_intra_L[which(pred_df_cxr_C$Te_intra_L<0)]<-0
pred_df_cxr_C$Tu_inter_L[which(pred_df_cxr_C$Tu_inter_L<0)]<-0
pred_df_cxr_C$Tu_intra_L[which(pred_df_cxr_C$Tu_intra_L<0)]<-0
which(pred_df_cxr_C$Te_inter_L>pred_df_cxr_C$Te_inter_U)
integer(0)
which(pred_df_cxr_C$Te_intra_L>pred_df_cxr_C$Te_intra_U)
integer(0)
pred_df_cxr_C[which(pred_df_cxr_C$Tu_inter_L>pred_df_cxr_C$Tu_inter_U),]
which(pred_df_cxr_C$Tu_intra_L>pred_df_cxr_C$Tu_intra_U)
integer(0)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="N" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="N" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="Cd" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="Cd" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific cadmium environment")
ca$Replicate<-as.character(ca$Rep)
subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="N" )
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="N" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="N" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific control environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_nested/pred_Tu_INTRA_Control_water.pdf", width=25, height=10)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="Cd" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="Cd" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific cadmium environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_nested/pred_Tu_INTRA_Control_cadmium.pdf", width=25, height=10)
#subset(pred_df_cxr_C, Tu_Regime==1 & Environment=="C" & Replicate==3)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Te_Regime=="SR4" & Environment=="N" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Te_Regime=="SR4" & Environment=="N" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="N" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. evansi")+
ggtitle("Te control intraspecific control environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_nested/pred_Te_INTRA_Control_control.pdf", width=25, height=10)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Te_Regime=="SR4" & Environment=="Cd" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Te_Regime=="SR4" & Environment=="Cd" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="Cd" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. evansi")+
ggtitle("Te control intraspecific cadmium environment")
save_plot("./NewAnalyses/cxr_lambda_fixed_nested/pred_Te_INTRA_Control_cadmium.pdf", width=25, height=10)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="N" & Te_Regime=="SR4"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="N" & Te_Regime=="SR4"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu & Te control interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="Cd" & Te_Regime=="SR4"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="Cd" & Te_Regime=="SR4"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu & Te control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Te_Regime=="SR4" & Environment=="N" & Tu_Regime=="SR1"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Te_Regime=="SR4" & Environment=="N" & Tu_Regime=="SR1"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="N" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te & Tu control intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Te_Regime=="SR4" & Environment=="Cd" & Tu_Regime=="SR1"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Te_Regime=="SR4" & Environment=="Cd" & Tu_Regime=="SR1"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="Cd" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te & Tu control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="N" & Te_Regime=="SR5"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="N" & Te_Regime=="SR5"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu control & Te evolved interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="Cd" & Te_Regime=="SR5"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Tu_Regime=="SR1" & Environment=="Cd" & Te_Regime=="SR5"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu control & Te evolved interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Te_Regime=="SR4" & Environment=="N" & Tu_Regime=="SR2"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Te_Regime=="SR4" & Environment=="N" & Tu_Regime=="SR2"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="N" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te control & Tu evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Te_Regime=="SR4" & Environment=="Cd" & Tu_Regime=="SR2"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Te_Regime=="SR4" & Environment=="Cd" & Tu_Regime=="SR2"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="Cd" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te control & Tu evolved interspecific cadmium environment")
ca$Replicate<-as.character(ca$Rep)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Tu_Regime=="SR2" & Environment=="N" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Tu_Regime=="SR2" & Environment=="N" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="N" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Tu_Regime=="SR2" & Environment=="Cd" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Tu_Regime=="SR2" & Environment=="Cd" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="Cd" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved intraspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Te_Regime=="SR5" & Environment=="N" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Te_Regime=="SR5" & Environment=="N" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="N" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Te_Regime=="SR5" & Environment=="Cd" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Te_Regime=="SR5" & Environment=="Cd" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="Cd" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved intraspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Tu_Regime=="SR2" & Environment=="N" & Te_Regime=="SR4"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Tu_Regime=="SR2" & Environment=="N" & Te_Regime=="SR4"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="N" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te control interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Tu_Regime=="SR2" & Environment=="Cd" & Te_Regime=="SR4"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Tu_Regime=="SR2" & Environment=="Cd" & Te_Regime=="SR4"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="Cd" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Te_Regime=="SR5" & Environment=="N" & Tu_Regime=="SR1"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Te_Regime=="SR5" & Environment=="N" & Tu_Regime=="SR1"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="N" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu control intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Te_Regime=="SR5" & Environment=="Cd" & Tu_Regime=="SR1"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Te_Regime=="SR5" & Environment=="Cd" & Tu_Regime=="SR1"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="Cd" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Tu_Regime=="SR2" & Environment=="N" & Te_Regime=="SR5"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Tu_Regime=="SR2" & Environment=="N" & Te_Regime=="SR5"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="N" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te evolved interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Tu_Regime=="SR2" & Environment=="Cd" & Te_Regime=="SR5"), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Tu_Regime=="SR2" & Environment=="Cd" & Te_Regime=="SR5"), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="Cd" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te evolved interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Te_Regime=="SR5" & Environment=="N" & Tu_Regime=="SR2"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Te_Regime=="SR5" & Environment=="N" & Tu_Regime=="SR2"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="N" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_cxr_C, Te_Regime=="SR5" & Environment=="Cd" & Tu_Regime=="SR2"), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_cxr_C, Te_Regime=="SR5" & Environment=="Cd" & Tu_Regime=="SR2"), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="Cd" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu evolved interspecific cadmium environment")
str(param_all_C)
'data.frame': 36 obs. of 10 variables:
$ Tu_Regime : chr "SR1" "SR2" "SR1" "SR2" ...
$ Te_Regime : chr "SR4" "SR4" "SR5" "SR5" ...
$ Replicate : int 1 1 1 1 2 2 3 3 3 3 ...
$ Environment: chr "N" "N" "N" "N" ...
$ Tu_lambda : num 3.9 3.7 3.9 3.7 4.2 ...
$ Te_lambda : num 11.11 11.11 10 10 9.86 ...
$ Tu_intra : num 0.1091 0.0765 0.1091 0.0765 0.1068 ...
$ Te_intra : num 0.144 0.144 0.1237 0.1237 0.0695 ...
$ Tu_inter : num 0.0794 0.098 0.0976 0.0979 0.0826 ...
$ Te_inter : num 0.209 0.175 0.15 0.203 0.157 ...
str(ca)
'data.frame': 3451 obs. of 42 variables:
$ Block : int 1 1 1 1 1 1 1 1 1 1 ...
$ Rep : int 1 1 1 1 1 1 1 1 1 1 ...
$ Box : int 1 1 1 1 1 1 1 1 1 1 ...
$ Leaf : int 3 4 3 4 3 4 3 4 3 4 ...
$ Disk : int 1 2 3 4 5 6 7 8 9 10 ...
$ Env : chr "N" "N" "Cd" "Cd" ...
$ FocalSR : int 4 4 4 4 4 4 4 4 4 4 ...
$ CompSR : int NA NA NA NA NA NA NA NA NA NA ...
$ Dens : int 1 1 1 1 2 2 2 2 4 4 ...
$ Type : chr "INTRA" "INTRA" "INTRA" "INTRA" ...
$ Focalfemale : chr "Te" "Te" "Te" "Te" ...
$ FocalDead : int 0 0 1 0 1 1 0 0 0 0 ...
$ FocalDrowned : int 0 0 0 0 0 0 0 0 0 0 ...
$ FocalMissing : int 0 0 0 0 0 0 0 0 0 0 ...
$ NumbDeadComp : int 0 0 0 0 0 0 0 0 0 0 ...
$ NumberOfEggs : int 11 21 3 9 15 16 17 11 58 24 ...
$ NumberOfEggsBelow : int 0 0 0 0 0 0 0 0 0 0 ...
$ TeMales : int 3 3 0 3 2 5 5 3 8 8 ...
$ TeFemales : int 6 9 2 5 2 10 4 4 9 6 ...
$ TeQuiescentfemales : int 0 0 0 0 0 0 3 0 3 4 ...
$ TuMales : int NA NA NA NA NA NA NA NA NA NA ...
$ TuFemales : int NA NA NA NA NA NA NA NA NA NA ...
$ TuQuiescentfemales : int NA NA NA NA NA NA NA NA NA NA ...
$ Observations : chr NA NA NA NA ...
$ Block2 : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
$ Rep2 : Factor w/ 5 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Disk2 : Factor w/ 16 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
$ Leaf2 : Factor w/ 2 levels "3","4": 1 2 1 2 1 2 1 2 1 2 ...
$ Env2 : Factor w/ 2 levels "Cd","N": 2 2 1 1 2 2 1 1 2 2 ...
$ FocalSR2 : Factor w/ 4 levels "1","2","4","5": 3 3 3 3 3 3 3 3 3 3 ...
$ CompSR2 : Factor w/ 4 levels "1","2","4","5": NA NA NA NA NA NA NA NA NA NA ...
$ Type2 : Factor w/ 2 levels "INTER","INTRA": 2 2 2 2 2 2 2 2 2 2 ...
$ Focal_Female2 : Factor w/ 2 levels "Te","Tu": 1 1 1 1 1 1 1 1 1 1 ...
$ Nr_Focal_Females_Tu_Alive_G0: num NA NA NA NA NA NA NA NA NA NA ...
$ Nr_Focal_Females_Te_Alive_G0: num 1 1 0 1 1 1 2 2 4 4 ...
$ Num_Comp_Tu_Alive_G0 : num NA NA NA NA NA NA NA NA NA NA ...
$ Num_Comp_Te_Alive_G0 : num NA NA NA NA NA NA NA NA NA NA ...
$ Nr_Focal_Females_G0 : num 1 1 1 1 2 2 2 2 4 4 ...
$ Nr_Comp_Females_G0 : num 0 0 0 0 1 1 1 1 3 3 ...
$ GrowthRateOA : num 6 9 2 5 1 5 2 2 2.25 1.5 ...
$ GrowthRateOA_Q : num 6 9 2 5 1 5 3.5 2 3 2.5 ...
$ Replicate : chr "1" "1" "1" "1" ...
rk_func<- function(lambda, alpha_ii, alpha_ij, dens_i, dens_j, ...){
gr<-lambda*exp(-alpha_ii*dens_i - alpha_ij*dens_j)
return(gr)
}
red_ca_C<-ca[,c("Env", "Rep", "FocalSR", "CompSR", "Dens", "Type", "TeFemales", "TuFemales", "GrowthRateOA")]
red_ca_C
red_ca_C$Dens_Focal<-sapply(c(1:length(red_ca_C[,1])), function(x){
if(red_ca_C$Type[x]=="INTRA"){
a<-red_ca_C$Dens[x]-1
}else if(red_ca_C$Type[x]=="INTER"){
a<-1
}
a
})
red_ca_C$Dens_Comp<-sapply(c(1:length(red_ca_C[,1])), function(x){
if(red_ca_C$Type[x]=="INTRA"){
a<-0
}else if(red_ca_C$Type[x]=="INTER"){
a<-red_ca_C$Dens[x]-1
}
a
})
red_ca_C$Focal<-mapvalues(red_ca_C$FocalSR, c(1,2,4,5), c("SR1", "SR2","SR4", "SR5"))
red_ca_C$Comp<-mapvalues(red_ca_C$CompSR, c(1,2,4,5), c("SR1", "SR2","SR4", "SR5"))
red_ca_C$pred<-sapply(c(1:length(red_ca_C[,1])), function(x){
if(red_ca_C$Focal[x]=="SR1" | red_ca_C$Focal[x]=="SR2"){
aux_data<-subset(param_all_C, Environment==red_ca_C$Env[x] & Replicate== red_ca_C$Rep[x] & as.character(Tu_Regime)==red_ca_C$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Tu_lambda[1], alpha_ii =aux_data$Tu_intra[1], alpha_ij = aux_data$Tu_inter[1], dens_i = red_ca_C$Dens_Focal[x], dens_j = red_ca_C$Dens_Comp[x])
}else if(red_ca_C$Focal[x]=="SR4" | red_ca_C$Focal[x]=="SR5"){
aux_data<-subset(param_all_C, Environment==red_ca_C$Env[x] & Replicate== red_ca_C$Rep[x] & as.character(Te_Regime)==red_ca_C$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Te_lambda[1], alpha_ii =aux_data$Te_intra[1], alpha_ij = aux_data$Te_inter[1], dens_i = red_ca_C$Dens_Focal[x], dens_j = red_ca_C$Dens_Comp[x])
}
aux_pred
})
red_ca_C$pred_L<-sapply(c(1:length(red_ca_C[,1])), function(x){
if(red_ca_C$Focal[x]=="SR1" | red_ca_C$Focal[x]=="SR2"){
aux_data<-subset(param_all_C_lower, Environment==red_ca_C$Env[x] & Replicate== red_ca_C$Rep[x] & as.character(Tu_Regime)==red_ca_C$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Tu_lambda[1], alpha_ii =aux_data$Tu_intra[1], alpha_ij = aux_data$Tu_inter[1], dens_i = red_ca_C$Dens_Focal[x], dens_j = red_ca_C$Dens_Comp[x])
}else if(red_ca_C$Focal[x]=="SR4" | red_ca_C$Focal[x]=="SR5"){
aux_data<-subset(param_all_C_lower, Environment==red_ca_C$Env[x] & Replicate== red_ca_C$Rep[x] & as.character(Te_Regime)==red_ca_C$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Te_lambda[1], alpha_ii =aux_data$Te_intra[1], alpha_ij = aux_data$Te_inter[1], dens_i = red_ca_C$Dens_Focal[x], dens_j = red_ca_C$Dens_Comp[x])
}
aux_pred
})
red_ca_C$pred_U<-sapply(c(1:length(red_ca_C[,1])), function(x){
if(red_ca_C$Focal[x]=="SR1" | red_ca_C$Focal[x]=="SR2"){
aux_data<-subset(param_all_C_upper, Environment==red_ca_C$Env[x] & Replicate== red_ca_C$Rep[x] & as.character(Tu_Regime)==red_ca_C$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Tu_lambda[1], alpha_ii =aux_data$Tu_intra[1], alpha_ij = aux_data$Tu_inter[1], dens_i = red_ca_C$Dens_Focal[x], dens_j = red_ca_C$Dens_Comp[x])
}else if(red_ca_C$Focal[x]=="SR4" | red_ca_C$Focal[x]=="SR5"){
aux_data<-subset(param_all_C_upper, Environment==red_ca_C$Env[x] & Replicate== red_ca_C$Rep[x] & as.character(Te_Regime)==red_ca_C$Focal[x])
aux_pred<-rk_func(lambda=aux_data$Te_lambda[1], alpha_ii =aux_data$Te_intra[1], alpha_ij = aux_data$Te_inter[1], dens_i = red_ca_C$Dens_Focal[x], dens_j = red_ca_C$Dens_Comp[x])
}
aux_pred
})
red_ca_C$Replicate<-red_ca_C$Rep
str(red_ca_C)
'data.frame': 3451 obs. of 17 variables:
$ Env : chr "N" "N" "Cd" "Cd" ...
$ Rep : int 1 1 1 1 1 1 1 1 1 1 ...
$ FocalSR : int 4 4 4 4 4 4 4 4 4 4 ...
$ CompSR : int NA NA NA NA NA NA NA NA NA NA ...
$ Dens : int 1 1 1 1 2 2 2 2 4 4 ...
$ Type : chr "INTRA" "INTRA" "INTRA" "INTRA" ...
$ TeFemales : int 6 9 2 5 2 10 4 4 9 6 ...
$ TuFemales : int NA NA NA NA NA NA NA NA NA NA ...
$ GrowthRateOA: num 6 9 2 5 1 5 2 2 2.25 1.5 ...
$ Dens_Focal : num 0 0 0 0 1 1 1 1 3 3 ...
$ Dens_Comp : num 0 0 0 0 0 0 0 0 0 0 ...
$ Focal : chr "SR4" "SR4" "SR4" "SR4" ...
$ Comp : chr NA NA NA NA ...
$ pred : num 11.11 11.11 3.3 3.3 9.62 ...
$ pred_L : num 6.41 6.41 1.41 1.41 5.67 ...
$ pred_U : num 15.81 15.81 5.19 5.19 13.4 ...
$ Replicate : int 1 1 1 1 1 1 1 1 1 1 ...
ggplot(subset(red_ca_C, Env=="N" & FocalSR==1), aes(x=log(GrowthRateOA+1), y=log(pred+1)))+
facet_grid(CompSR~Replicate)+
geom_abline(slope = 1, intercept=0)+
geom_point()+
ylab("Predicted")+
xlab("Observed")+
ggtitle("Normal environment Control urticae")
ggplot(subset(red_ca_C, Env=="N" & FocalSR==1), aes(x=GrowthRateOA, y=pred))+
facet_grid(CompSR~Replicate)+
geom_abline(slope = 1, intercept=0)+
geom_point()+
ylab("Predicted")+
xlab("Observed")+
ggtitle("Normal environment Control urticae")
NA
NA
NA
# creating folder to put the analyses inside, this should be the same as the file path in the function
dir.create("./NewAnalyses/Optim_normal", showWarnings = FALSE)
source("./function_riker_27May.R")
# This matrix has all the comparisons that need to be done between regimes
comparison_mat<-matrix(nrow=4, ncol=3)
comparison_mat[1,]<-c(1,4,5)
comparison_mat[2,]<-c(2,4,5)
comparison_mat[3,]<-c(4,1,2)
comparison_mat[4,]<-c(5,1,2)
#lam2 is the data from density one corresponding to the focals populations
# data2 is the data (format) Regime (name of focal pop), background (name of competitor, the same if its intraspecific competition), focal (number of focal individuals in g0), comp (number of competitors in g0), growth rate
# Attention that for intraspecific you need to add 0 in the comp and all individuals in the focal
rep2<-mod_df(subset(ca,Rep==1 & Env=="N"))
magic_rk(filepath2 = "./NewAnalyses/Optim_normal/",data2=rep2, reps2=1, env="N", comparisons = comparison_mat)
[1] "5 model 0 converged on rep 1"
Warning: NaNs produced
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==1 & Env=="Cd"))
magic_rk(filepath2 = "./NewAnalyses/Optim_normal/", lam2=dataForLambda, data2=rep2, reps2=1, env="Cd", comparisons = comparison_mat)
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==3 & Env=="N"))
magic_rk(filepath2 = "./NewAnalyses/Optim_normal/", lam2=dataForLambda, data2=rep2, reps2=3, env="N", comparisons = comparison_mat)
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==3 & Env=="Cd"))
magic_rk(filepath2 = "./NewAnalyses/Optim_normal/", lam2=dataForLambda, data2=rep2, reps2=3, env="Cd", comparisons = comparison_mat)
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==4 & Env=="N"))
magic_rk(filepath2 = "./NewAnalyses/Optim_normal/", lam2=dataForLambda, data2=rep2, reps2=4, env="N", comparisons = comparison_mat)
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==4 & Env=="Cd"))
magic_rk(filepath2 = "./NewAnalyses/Optim_normal/", lam2=dataForLambda, data2=rep2, reps2=4, env="Cd", comparisons = comparison_mat)
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==5 & Env=="N"))
magic_rk(filepath2 = "./NewAnalyses/Optim_normal/", lam2=dataForLambda, data2=rep2, reps2=5, env="N", comparisons = comparison_mat)
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==5 & Env=="Cd"))
magic_rk(filepath2 = "./NewAnalyses/Optim_normal/", lam2=dataForLambda, data2=rep2, reps2=5, env="Cd", comparisons = comparison_mat)
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 0 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
# For two we have to change the comparison matrix
comparison_mat2<-matrix(nrow=3, ncol=3)
comparison_mat2[1,]<-c(1,4,5)
comparison_mat2[2,]<-c(4,1,NA)
comparison_mat2[3,]<-c(5,1,NA)
rep2<-mod_df(subset(ca,Rep==2 & Env=="N"))
magic_rk(filepath2 = "./NewAnalyses/Optim_normal/", lam2=dataForLambda, data2=rep2, reps2=2, env="N", comparisons = comparison_mat2)
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "1 model 0 converged on rep 1"
[1] "1 model 2 converged on rep 1"
[1] "1 model 3 converged on rep 1"
[1] "1 model 0 converged on rep 1"
[1] "1 model 2 converged on rep 1"
[1] "1 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==2 & Env=="Cd"))
magic_rk(filepath2 = "./NewAnalyses/Optim_normal/", lam2=dataForLambda, data2=rep2, reps2=2, env="Cd", comparisons = comparison_mat2)
[1] "5 model 0 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "1 model 0 converged on rep 1"
[1] "1 model 2 converged on rep 1"
[1] "1 model 3 converged on rep 1"
[1] "1 model 0 converged on rep 1"
[1] "1 model 2 converged on rep 1"
[1] "1 model 3 converged on rep 1"
alpha_file<-list.files("./NewAnalyses/Optim_normal/", pattern="alpha_estimates") #the alphas are always tu, te (row), tu, te (col)
alphaUpper_file<-list.files("./NewAnalyses/Optim_normal/", pattern="alpha_upper")
alphaLower_file<-list.files("./NewAnalyses/Optim_normal/", pattern="alpha_lower")
lambda_file<-list.files("./NewAnalyses/Optim_normal/", pattern="lambda_estimates")
alpha_list<- lapply(alpha_file, function(x) read.csv(paste("./NewAnalyses/Optim_normal/",x, sep=""), header = TRUE))
alphaUpper_list<- lapply(alphaUpper_file, function(x) read.csv(paste("./NewAnalyses/Optim_normal/",x, sep=""), header = TRUE))
alphaLower_list<- lapply(alphaLower_file, function(x) read.csv(paste("./NewAnalyses/Optim_normal/",x, sep=""), header = TRUE))
lambda_list<- lapply(lambda_file, function(x) read.csv(paste("./NewAnalyses/Optim_normal/",x, sep=""), header = TRUE))
# passing from list to data frame
# First we need to do the first iteration (to create everything)
lambda_intra_fixed<-data.frame(Regime1=rep(c(1,1,2,2),10), Regime2=rep(c(4,5,4,5), 10), Replicate=c(rep(1,8),rep(2,8),rep(3,8),rep(4,8),rep(5,8)), Env=rep(c(rep("N",4), rep("Cd",4)), 5))
lambda_intra_fixed<-lambda_intra_fixed[-which(lambda_intra_fixed$Regime1==2 & lambda_intra_fixed$Replicate==2),] # to remove SR2 from replicate 2 because it does not exist
alpha_list[[1]]
lambda_list[[1]]
# passing alphas to dataframe
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[1], split="_")[1])[6], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[1], split="_")[1])[6], split="[.]"))[1],split=""))[2]
regimeTu<-c("1","1", "2","2")
regimeTe<-c("4","5", "4","5")
Env<-rep(env, 4)
Rep<-rep(repli,4)
aux_alpha<-as.data.frame(alpha_list[[1]])
aux_alpha2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu=c(aux_alpha[1,2], aux_alpha[1,2], aux_alpha[2,2],aux_alpha[2,2]), intraTe=c(aux_alpha[3,2], aux_alpha[4,2], aux_alpha[3,2],aux_alpha[4,2]), interTu=c(aux_alpha[1,3], aux_alpha[1,4], aux_alpha[2,3], aux_alpha[2,4]), interTe=c(aux_alpha[3,3], aux_alpha[4,3], aux_alpha[3,4], aux_alpha[4,4]))
for(x in 2:length(lambda_list)){
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[x], split="_")[1])[6], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[x], split="_")[1])[6], split="[.]"))[1],split=""))[2]
if(x==3 | x==4){# because there is no SR2 here
regimeTu<-c("1","1")
regimeTe<-c("4","5")
Env<-rep(env, 2)
Rep<-rep(repli,2)
aux_alpha<-as.data.frame(alpha_list[[x]])
aux2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu=c(aux_alpha[1,2], aux_alpha[1,2]), intraTe=c(aux_alpha[2,2], aux_alpha[3,2]), interTu=c(aux_alpha[1,3], aux_alpha[1,4]), interTe=c(aux_alpha[2,3], aux_alpha[3,3]))
}else{
regimeTu<-c("1","1", "2","2")
regimeTe<-c("4","5", "4","5")
Env<-rep(env, 4)
Rep<-rep(repli,4)
aux_alpha<-as.data.frame(alpha_list[[x]])
aux2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu=c(aux_alpha[1,2], aux_alpha[1,2], aux_alpha[2,2],aux_alpha[2,2]), intraTe=c(aux_alpha[3,2], aux_alpha[4,2], aux_alpha[3,2],aux_alpha[4,2]), interTu=c(aux_alpha[1,3], aux_alpha[1,4], aux_alpha[2,3], aux_alpha[2,4]), interTe=c(aux_alpha[3,3], aux_alpha[4,3], aux_alpha[3,4], aux_alpha[4,4]))
}
aux_alpha2<-rbind(aux_alpha2, aux2)
}
### Alpha Lower
# passing alphas to dataframe
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaLower_file[1], split="_")[1])[4], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaLower_file[1], split="_")[1])[4], split="[.]"))[1],split=""))[2]
regimeTu<-c("1","1", "2","2")
regimeTe<-c("4","5", "4","5")
Env<-rep(env, 4)
Rep<-rep(repli,4)
aux_alphaLower<-as.data.frame(alphaLower_list[[1]])
aux_alphaLower2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu_L=c(aux_alphaLower[1,2], aux_alphaLower[1,2], aux_alphaLower[2,2],aux_alphaLower[2,2]), intraTe_L=c(aux_alphaLower[3,2], aux_alphaLower[4,2], aux_alphaLower[3,2],aux_alphaLower[4,2]), interTu_L=c(aux_alphaLower[1,3], aux_alphaLower[1,4], aux_alphaLower[2,3], aux_alphaLower[2,4]), interTe_L=c(aux_alphaLower[3,3], aux_alphaLower[4,3], aux_alphaLower[3,4], aux_alphaLower[4,4]))
for(x in 2:length(lambda_list)){
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaLower_file[x], split="_")[1])[4], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaLower_file[x], split="_")[1])[4], split="[.]"))[1],split=""))[2]
if(x==3 | x==4){# because there is no SR2 here
regimeTu<-c("1","1")
regimeTe<-c("4","5")
Env<-rep(env, 2)
Rep<-rep(repli,2)
aux_alphaLower<-as.data.frame(alphaLower_list[[x]])
aux2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu_L=c(aux_alphaLower[1,2], aux_alphaLower[1,2]), intraTe_L=c(aux_alphaLower[2,2], aux_alphaLower[3,2]), interTu_L=c(aux_alphaLower[1,3], aux_alphaLower[1,4]), interTe_L=c(aux_alphaLower[2,3], aux_alphaLower[3,3]))
}else{
regimeTu<-c("1","1", "2","2")
regimeTe<-c("4","5", "4","5")
Env<-rep(env, 4)
Rep<-rep(repli,4)
aux_alphaLower<-as.data.frame(alphaLower_list[[x]])
aux2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu_L=c(aux_alphaLower[1,2], aux_alphaLower[1,2], aux_alphaLower[2,2],aux_alphaLower[2,2]), intraTe_L=c(aux_alphaLower[3,2], aux_alphaLower[4,2], aux_alphaLower[3,2],aux_alphaLower[4,2]), interTu_L=c(aux_alphaLower[1,3], aux_alphaLower[1,4], aux_alphaLower[2,3], aux_alphaLower[2,4]), interTe_L=c(aux_alphaLower[3,3], aux_alphaLower[4,3], aux_alphaLower[3,4], aux_alphaLower[4,4]))
}
aux_alphaLower2<-rbind(aux_alphaLower2, aux2)
}
### Alpha Upper
# passing alphas to dataframe
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaUpper_file[1], split="_")[1])[4], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaUpper_file[1], split="_")[1])[4], split="[.]"))[1],split=""))[2]
regimeTu<-c("1","1", "2","2")
regimeTe<-c("4","5", "4","5")
Env<-rep(env, 4)
Rep<-rep(repli,4)
aux_alphaUpper<-as.data.frame(alphaUpper_list[[1]])
aux_alphaUpper2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu_U=c(aux_alphaUpper[1,2], aux_alphaUpper[1,2], aux_alphaUpper[2,2],aux_alphaUpper[2,2]), intraTe_U=c(aux_alphaUpper[3,2], aux_alphaUpper[4,2], aux_alphaUpper[3,2],aux_alphaUpper[4,2]), interTu_U=c(aux_alphaUpper[1,3], aux_alphaUpper[1,4], aux_alphaUpper[2,3], aux_alphaUpper[2,4]), interTe_U=c(aux_alphaUpper[3,3], aux_alphaUpper[4,3], aux_alphaUpper[3,4], aux_alphaUpper[4,4]))
for(x in 2:length(lambda_list)){
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaUpper_file[x], split="_")[1])[4], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaUpper_file[x], split="_")[1])[4], split="[.]"))[1],split=""))[2]
if(x==3 | x==4){# because there is no SR2 here
regimeTu<-c("1","1")
regimeTe<-c("4","5")
Env<-rep(env, 2)
Rep<-rep(repli,2)
aux_alphaUpper<-as.data.frame(alphaUpper_list[[x]])
aux2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu_U=c(aux_alphaUpper[1,2], aux_alphaUpper[1,2]), intraTe_U=c(aux_alphaUpper[2,2], aux_alphaUpper[3,2]), interTu_U=c(aux_alphaUpper[1,3], aux_alphaUpper[1,4]), interTe_U=c(aux_alphaUpper[2,3], aux_alphaUpper[3,3]))
}else{
regimeTu<-c("1","1", "2","2")
regimeTe<-c("4","5", "4","5")
Env<-rep(env, 4)
Rep<-rep(repli,4)
aux_alphaUpper<-as.data.frame(alphaUpper_list[[x]])
aux2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu_U=c(aux_alphaUpper[1,2], aux_alphaUpper[1,2], aux_alphaUpper[2,2],aux_alphaUpper[2,2]), intraTe_U=c(aux_alphaUpper[3,2], aux_alphaUpper[4,2], aux_alphaUpper[3,2],aux_alphaUpper[4,2]), interTu_U=c(aux_alphaUpper[1,3], aux_alphaUpper[1,4], aux_alphaUpper[2,3], aux_alphaUpper[2,4]), interTe_U=c(aux_alphaUpper[3,3], aux_alphaUpper[4,3], aux_alphaUpper[3,4], aux_alphaUpper[4,4]))
}
aux_alphaUpper2<-rbind(aux_alphaUpper2, aux2)
}
# Passing lambda to data frame
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[1], split="_")[1])[6], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[1], split="_")[1])[6], split="[.]"))[1],split=""))[2]
Focal<-c("1","1","2","2","4","4","5","5")
Comp<-c("4","5","4","5","1","2","1","2")
Env<-rep(env, 8)
Rep<-rep(repli,8)
aux_lambda<-cbind(as.data.frame(lambda_list[[1]])[,c(3,4,5)],Focal,Comp, Env, Rep)
for(x in 2:length(lambda_list)){
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[x], split="_")[1])[6], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[x], split="_")[1])[6], split="[.]"))[1],split=""))[2]
if(x==3 | x==4){# because there is no SR2 here
Focal<-c("1","1","4","5")
Comp<-c("4","5","1","1")
Env<-rep(env, 4)
Rep<-rep(repli,4)
aux<-cbind(as.data.frame(lambda_list[[x]])[,c(3,4,5)],Focal,Comp, Env, Rep)
}else{
Focal<-c("1","1","2","2","4","4","5","5")
Comp<-c("4","5","4","5","1","2","1","2")
Env<-rep(env, 8)
Rep<-rep(repli,8)
aux<-cbind(as.data.frame(lambda_list[[x]])[,c(3,4,5)],Focal,Comp, Env, Rep)
}
aux_lambda<-rbind(aux_lambda, aux)
}
#Matching all the data
alphas_mat_D<-as.data.frame(cbind(aux_alpha2, aux_alphaLower2, aux_alphaUpper2))
str(lambda_intra_fixed)
'data.frame': 36 obs. of 4 variables:
$ Regime1 : num 1 1 2 2 1 1 2 2 1 1 ...
$ Regime2 : num 4 5 4 5 4 5 4 5 4 5 ...
$ Replicate: num 1 1 1 1 1 1 1 1 2 2 ...
$ Env : chr "N" "N" "N" "N" ...
#### adding lambda
alphas_mat_D$lambdaTu<-sapply(c(1:length(alphas_mat_D[,1])), function(x){
auxi<-subset(aux_lambda, (Focal==alphas_mat_D$regimeTu[x] & Comp==alphas_mat_D$regimeTe[x]) & Rep==alphas_mat_D$Rep[x] & Env==alphas_mat_D$Env[x] )
auxi[1,1]
})
alphas_mat_D$lambdaTe<-sapply(c(1:length(alphas_mat_D[,1])), function(x){
auxi<-subset(aux_lambda, (Focal==alphas_mat_D$regimeTe[x] & Comp==alphas_mat_D$regimeTu[x]) & Rep==alphas_mat_D$Rep[x] & Env==alphas_mat_D$Env[x] )
auxi[1,1]
})
alphas_mat_D$lambdaTu_L<-sapply(c(1:length(alphas_mat_D[,1])), function(x){
auxi<-subset(aux_lambda, (Focal==alphas_mat_D$regimeTu[x] & Comp==alphas_mat_D$regimeTe[x]) & Rep==alphas_mat_D$Rep[x] & Env==alphas_mat_D$Env[x] )
auxi[1,2]
})
alphas_mat_D$lambdaTe_L<-sapply(c(1:length(alphas_mat_D[,1])), function(x){
auxi<-subset(aux_lambda, (Focal==alphas_mat_D$regimeTe[x] & Comp==alphas_mat_D$regimeTu[x]) & Rep==alphas_mat_D$Rep[x] & Env==alphas_mat_D$Env[x] )
auxi[1,2]
})
alphas_mat_D$lambdaTu_U<-sapply(c(1:length(alphas_mat_D[,1])), function(x){
auxi<-subset(aux_lambda, (Focal==alphas_mat_D$regimeTu[x] & Comp==alphas_mat_D$regimeTe[x]) & Rep==alphas_mat_D$Rep[x] & Env==alphas_mat_D$Env[x] )
auxi[1,3]
})
alphas_mat_D$lambdaTe_U<-sapply(c(1:length(alphas_mat_D[,1])), function(x){
auxi<-subset(aux_lambda, (Focal==alphas_mat_D$regimeTe[x] & Comp==alphas_mat_D$regimeTu[x]) & Rep==alphas_mat_D$Rep[x] & Env==alphas_mat_D$Env[x] )
auxi[1,3]
})
alphas_mat_D$Env2<-mapvalues(alphas_mat_D$Env, c("C","N"), c("Cd","N"))
# clean up the matrix, because it has a lot of repeated columns
alphas_mat_D<-alphas_mat_D[,c(1:8, 13:16,21:30)]
alphas_mat_D
NA
### Plotting data
colnames(alphas_mat_D)<-c("Tu_Regime", "Te_Regime", "Environment", "Replicate", "Tu_intra", "Te_intra", "Tu_inter", "Te_inter", "Tu_intra_L", "Te_intra_L", "Tu_inter_L", "Te_inter_L", "Tu_intra_U", "Te_intra_U", "Tu_inter_U", "Te_inter_U", "Tu_lambda", "Te_lambda","Tu_lambda_L", "Te_lambda_L","Tu_lambda_U", "Te_lambda_U")
alphas_mat_D_long<-gather(alphas_mat_D, parameter, value,Tu_intra:Te_lambda_U )
alphas_mat_D_long$category<-mapvalues(alphas_mat_D_long$parameter, c("Tu_intra", "Te_intra", "Tu_inter", "Te_inter", "Tu_intra_L", "Te_intra_L", "Tu_inter_L", "Te_inter_L", "Tu_intra_U", "Te_intra_U", "Tu_inter_U", "Te_inter_U", "Tu_lambda", "Te_lambda","Tu_lambda_L", "Te_lambda_L","Tu_lambda_U", "Te_lambda_U"), c("intra", "intra", "inter", "inter", "intra_L", "intra_L", "inter_L", "inter_L","intra_U", "intra_U", "inter_U", "inter_U","lambda","lambda","lambda_L","lambda_L","lambda_U","lambda_U"))
str(alphas_mat_D_long)
'data.frame': 648 obs. of 7 variables:
$ Tu_Regime : chr "1" "1" "2" "2" ...
$ Te_Regime : chr "4" "5" "4" "5" ...
$ Environment: chr "C" "C" "C" "C" ...
$ Replicate : chr "1" "1" "1" "1" ...
$ parameter : chr "Tu_intra" "Tu_intra" "Tu_intra" "Tu_intra" ...
$ value : num 0.0252 0.0252 0.0285 0.0285 0.0785 ...
$ category : chr "intra" "intra" "intra" "intra" ...
###########
ggplot(subset(alphas_mat_D_long, parameter=="Te_intra" & Tu_Regime=="1" & Environment=="C"), aes(x=Te_Regime, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Environment")
save_plot("./NewAnalyses/Optim_normal/26May_RK_intraTe_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(alphas_mat_D_long, parameter=="Te_intra" & Tu_Regime=="1" & Environment=="N"), aes(x=Te_Regime, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Environment")
save_plot("./NewAnalyses/Optim_normal/26May_RK_intraTe_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(alphas_mat_D_long, parameter=="Tu_intra" & Te_Regime=="4" & Environment=="C"), aes(fill=Tu_Regime, y=value, x=Tu_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Environment")
save_plot("./NewAnalyses/Optim_normal/26May_RK_intraTu_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(alphas_mat_D_long, parameter=="Tu_intra" & Te_Regime=="4" & Environment=="N"), aes(fill=Tu_Regime, y=value, x=Tu_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Environment")
save_plot("./NewAnalyses/Optim_normal/26May_RK_intraTu_w0_rep_N.pdf", width=20, height=15)
######## INTER
ggplot(subset(alphas_mat_D_long, parameter=="Te_inter" & Environment=="C"), aes(x=Te_Regime, y=value, fill=Te_Regime))+
facet_grid(.~Tu_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_x_discrete(labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. evansi)")+
xlab("Selection regime T. evansi")+
ggtitle("Environment: cadmium")
save_plot("./NewAnalyses/Optim_normal/26May_RK_interTe_w0_rep_Cd.pdf", width=20, height=15)
ggplot(subset(alphas_mat_D_long, parameter=="Te_inter" & Environment=="N"), aes(x=Te_Regime, y=value, fill=Te_Regime))+
facet_grid(.~Tu_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_x_discrete(labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. evansi)")+
xlab("Selection regime T. evansi")+
ggtitle("Environment: control")
save_plot("./NewAnalyses/Optim_normal/26May_RK_interTe_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(alphas_mat_D_long, parameter=="Tu_inter" & Environment=="C"), aes(fill=Tu_Regime, y=value, x=Tu_Regime))+
facet_grid(.~Te_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot(outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.75), aes(shape=Replicate))+
theme_ines+
scale_x_discrete(labels=c("Control", "Evolved"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. urticae)")+
xlab("Selection regime T. urticae")+
ggtitle("Environment: cadmium")
save_plot("./NewAnalyses/Optim_normal/26May_RK_interTu_w0_rep_Cd.pdf", width=20, height=15)
ggplot(subset(alphas_mat_D_long, parameter=="Tu_inter" & Environment=="N"), aes(fill=Tu_Regime, y=value, x=Tu_Regime))+
facet_grid(.~Te_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot(outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.75), aes(shape=Replicate))+
theme_ines+
scale_x_discrete(labels=c("Control", "Evolved"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. urticae)")+
xlab("Selection regime T. urticae")+
ggtitle("Environment: control")
save_plot("./NewAnalyses/Optim_normal/26May_RK_interTu_w0_rep_N.pdf", width=20, height=15)
######### Lambda
ggplot(subset(alphas_mat_D_long, parameter=="Te_lambda" & Tu_Regime=="1" & Environment=="C"), aes(x=Te_Regime, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. evansi)")+
xlab("Selection regime T. evansi")+
ggtitle("Environment: cadmium")
save_plot("./NewAnalyses/Optim_normal/26May_RK_lambdaTe_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(alphas_mat_D_long, parameter=="Te_lambda" & Tu_Regime=="1" & Environment=="N"), aes(x=Te_Regime, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. evansi)")+
xlab("Selection regime T. evansi")+
ggtitle("Environment: control")
save_plot("./NewAnalyses/Optim_normal/26May_RK_lambdaTe_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(alphas_mat_D_long, parameter=="Tu_lambda" & Te_Regime=="4" & Environment=="C"), aes(fill=Tu_Regime, y=value, x=Tu_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. urticae)")+
xlab("Selection regime T. urticae")+
ggtitle("Environment: cadmium")
save_plot("./NewAnalyses/Optim_normal/26May_RK_lambdaTu_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(alphas_mat_D_long, parameter=="Tu_lambda" & Te_Regime=="4" & Environment=="N"), aes(fill=Tu_Regime, y=value, x=Tu_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. urticae)")+
xlab("Selection regime T. urticae")+
ggtitle("Environment: control")
save_plot("./NewAnalyses/Optim_normal/26May_RK_lambdaTu_w0_rep_N.pdf", width=20, height=15)
NA
NA
str(alphas_mat_D)
'data.frame': 36 obs. of 22 variables:
$ Tu_Regime : chr "1" "1" "2" "2" ...
$ Te_Regime : chr "4" "5" "4" "5" ...
$ Environment: chr "C" "C" "C" "C" ...
$ Replicate : chr "1" "1" "1" "1" ...
$ Tu_intra : num 0.0252 0.0252 0.0285 0.0285 0.0785 ...
$ Te_intra : num 0.0778 0.0914 0.0778 0.0914 0.1529 ...
$ Tu_inter : num 0.04392 0.03232 0.00834 0.0142 0.03398 ...
$ Te_inter : num 0.0273 0.072 0.0596 0.0419 0.1961 ...
$ Tu_intra_L : num -0.00367 -0.00367 -0.00163 -0.00163 0.04236 ...
$ Te_intra_L : num 0.0472 0.052 0.0472 0.052 0.1151 ...
$ Tu_inter_L : num 0.0231 0.0101 -0.0232 -0.018 -0.0265 ...
$ Te_inter_L : num -0.03664 0.02552 0.00625 -0.01089 0.11837 ...
$ Tu_intra_U : num 0.054 0.054 0.0586 0.0586 0.1146 ...
$ Te_intra_U : num 0.109 0.131 0.109 0.131 0.191 ...
$ Tu_inter_U : num 0.0647 0.0545 0.0398 0.0464 0.0945 ...
$ Te_inter_U : num 0.0913 0.1184 0.1129 0.0947 0.2738 ...
$ Tu_lambda : num 0.0252 0.0252 0.0285 0.0285 0.0785 ...
$ Te_lambda : num 0.0778 0.0914 0.0778 0.0914 0.1529 ...
$ Tu_lambda_L: num 1.007 1.007 0.795 0.795 1.421 ...
$ Te_lambda_L: num 1.682 0.869 1.682 0.869 7.339 ...
$ Tu_lambda_U: num 1.82 1.82 2.01 2.01 4.31 ...
$ Te_lambda_U: num 3.84 5.53 3.84 5.53 13.05 ...
alphas_mat_D$Env2<-mapvalues(alphas_mat_D$Environment, c("C", "N"), c("Cd","N"))
str(ca)
'data.frame': 3451 obs. of 42 variables:
$ Block : int 1 1 1 1 1 1 1 1 1 1 ...
$ Rep : int 1 1 1 1 1 1 1 1 1 1 ...
$ Box : int 1 1 1 1 1 1 1 1 1 1 ...
$ Leaf : int 3 4 3 4 3 4 3 4 3 4 ...
$ Disk : int 1 2 3 4 5 6 7 8 9 10 ...
$ Env : chr "N" "N" "Cd" "Cd" ...
$ FocalSR : int 4 4 4 4 4 4 4 4 4 4 ...
$ CompSR : int NA NA NA NA NA NA NA NA NA NA ...
$ Dens : int 1 1 1 1 2 2 2 2 4 4 ...
$ Type : chr "INTRA" "INTRA" "INTRA" "INTRA" ...
$ Focalfemale : chr "Te" "Te" "Te" "Te" ...
$ FocalDead : int 0 0 1 0 1 1 0 0 0 0 ...
$ FocalDrowned : int 0 0 0 0 0 0 0 0 0 0 ...
$ FocalMissing : int 0 0 0 0 0 0 0 0 0 0 ...
$ NumbDeadComp : int 0 0 0 0 0 0 0 0 0 0 ...
$ NumberOfEggs : int 11 21 3 9 15 16 17 11 58 24 ...
$ NumberOfEggsBelow : int 0 0 0 0 0 0 0 0 0 0 ...
$ TeMales : int 3 3 0 3 2 5 5 3 8 8 ...
$ TeFemales : int 6 9 2 5 2 10 4 4 9 6 ...
$ TeQuiescentfemales : int 0 0 0 0 0 0 3 0 3 4 ...
$ TuMales : int NA NA NA NA NA NA NA NA NA NA ...
$ TuFemales : int NA NA NA NA NA NA NA NA NA NA ...
$ TuQuiescentfemales : int NA NA NA NA NA NA NA NA NA NA ...
$ Observations : chr NA NA NA NA ...
$ Block2 : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
$ Rep2 : Factor w/ 5 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Disk2 : Factor w/ 16 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
$ Leaf2 : Factor w/ 2 levels "3","4": 1 2 1 2 1 2 1 2 1 2 ...
$ Env2 : Factor w/ 2 levels "Cd","N": 2 2 1 1 2 2 1 1 2 2 ...
$ FocalSR2 : Factor w/ 4 levels "1","2","4","5": 3 3 3 3 3 3 3 3 3 3 ...
$ CompSR2 : Factor w/ 4 levels "1","2","4","5": NA NA NA NA NA NA NA NA NA NA ...
$ Type2 : Factor w/ 2 levels "INTER","INTRA": 2 2 2 2 2 2 2 2 2 2 ...
$ Focal_Female2 : Factor w/ 2 levels "Te","Tu": 1 1 1 1 1 1 1 1 1 1 ...
$ Nr_Focal_Females_Tu_Alive_G0: num NA NA NA NA NA NA NA NA NA NA ...
$ Nr_Focal_Females_Te_Alive_G0: num 1 1 0 1 1 1 2 2 4 4 ...
$ Num_Comp_Tu_Alive_G0 : num NA NA NA NA NA NA NA NA NA NA ...
$ Num_Comp_Te_Alive_G0 : num NA NA NA NA NA NA NA NA NA NA ...
$ Nr_Focal_Females_G0 : num 1 1 1 1 2 2 2 2 4 4 ...
$ Nr_Comp_Females_G0 : num 0 0 0 0 1 1 1 1 3 3 ...
$ GrowthRateOA : num 6 9 2 5 1 5 2 2 2.25 1.5 ...
$ GrowthRateOA_Q : num 6 9 2 5 1 5 3.5 2 3 2.5 ...
$ Replicate : chr "1" "1" "1" "1" ...
# Since the lambda is from the log data
ca$pred_D<-sapply(c(1:length(ca$Block)), function(x){
if(ca$Focalfemale[x]=="Tu"){
alpha_i<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Tu_Regime==as.character(ca$FocalSR[x]) & Replicate==ca$Rep[x])$Tu_intra[1]
alpha_ij<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Te_Regime==ca$CompSR[x] & Replicate==ca$Rep[x])$Tu_inter[1]
lambda<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Tu_lambda[1]
}else if(ca$Focalfemale[x]=="Te"){
alpha_i<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Te_intra[1]
alpha_ij<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Tu_Regime==ca$CompSR[x] & Replicate==ca$Rep[x])$Te_inter[1]
lambda<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Te_lambda[1]
}
if(ca$Type[x]=="INTRA"){
densF<-ca$Dens[x]
pred<-lambda*exp(-alpha_i*(densF))
}else if(ca$Type[x]=="INTER"){
densC<-ca$Dens[x]
pred<-lambda*exp(-alpha_ij*densC)
}
pred
})
x<-1
ca$pred_D_L<-sapply(c(1:length(ca$Block)), function(x){
if(ca$Focalfemale[x]=="Tu"){
alpha_i<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Tu_intra_L[1]
alpha_ij<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Te_Regime==ca$CompSR[x] & Replicate==ca$Rep[x])$Tu_inter_L[1]
lambda<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Tu_lambda_L[1]
}else if(ca$Focalfemale[x]=="Te"){
alpha_i<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Te_intra_L[1]
alpha_ij<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Tu_Regime==ca$CompSR[x] & Replicate==ca$Rep[x])$Te_inter_L[1]
lambda<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Te_lambda_L[1]
}
if(ca$Type[x]=="INTRA"){
densF<-ca$Dens[x]
pred<-lambda*exp(-alpha_i*(densF-1))
}else if(ca$Type[x]=="INTER"){
densC<-ca$Dens[x]-1
pred<-lambda*exp(-alpha_ij*densC)
}
pred
})
ca$pred_D_U<-sapply(c(1:length(ca$Block)), function(x){
if(ca$Focalfemale[x]=="Tu"){
alpha_i<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Tu_intra_U[1]
alpha_ij<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Te_Regime==ca$CompSR[x] & Replicate==ca$Rep[x])$Tu_inter_U[1]
lambda<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Tu_lambda_U[1]
}else if(ca$Focalfemale[x]=="Te"){
alpha_i<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Te_intra_U[1]
alpha_ij<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Tu_Regime==ca$CompSR[x] & Replicate==ca$Rep[x])$Te_inter_U[1]
lambda<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Te_lambda_U[1]
}
if(ca$Type[x]=="INTRA"){
densF<-ca$Dens[x]
pred<-lambda*exp(-alpha_i*(densF-1))
}else if(ca$Type[x]=="INTER"){
densC<-ca$Dens[x]-1
pred<-lambda*exp(-alpha_ij*densC)
}
pred
})
density_aux<-seq(0, 10, by=(10/100))
pred_df_D<-as.data.frame(expand_grid(Density=density_aux, Tu_Regime=c(1,2), Te_Regime=c(4,5), Replicate=c(1:5), Environment=c("N", "C")))
pred_df_D$Tu_mean_intra<-sapply(c(1:length(pred_df_D[,1])), function(x){
alpha_i<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_inter[1]
lambda<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_D$Density[x])
pred
})
pred_df_D$Tu_mean_inter<-sapply(c(1:length(pred_df_D[,1])), function(x){
alpha_i<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_inter[1]
lambda<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_D$Density[x])
pred
})
pred_df_D$Tu_intra_L<-sapply(c(1:length(pred_df_D[,1])), function(x){
alpha_i<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_intra_L[1]
alpha_ij<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_inter_L[1]
lambda<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_lambda_L[1]
pred<-lambda*exp(-alpha_i*pred_df_D$Density[x])
pred
})
pred_df_D$Tu_inter_L<-sapply(c(1:length(pred_df_D[,1])), function(x){
alpha_i<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_intra_L[1]
alpha_ij<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_inter_L[1]
lambda<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_lambda_L[1]
pred<-lambda*exp(-alpha_ij*pred_df_D$Density[x])
pred
})
pred_df_D$Tu_intra_U<-sapply(c(1:length(pred_df_D[,1])), function(x){
alpha_i<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_intra_U[1]
alpha_ij<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_inter_U[1]
lambda<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_lambda_U[1]
pred<-lambda*exp(-alpha_i*pred_df_D$Density[x])
pred
})
pred_df_D$Tu_inter_U<-sapply(c(1:length(pred_df_D[,1])), function(x){
alpha_i<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_intra_U[1]
alpha_ij<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_inter_U[1]
lambda<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Replicate==pred_df_D$Replicate[x])$Tu_lambda_U[1]
pred<-lambda*exp(-alpha_ij*pred_df_D$Density[x])
pred
})
pred_df_D$Te_mean_intra<-sapply(c(1:length(pred_df_D[,1])), function(x){
alpha_i<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_intra[1]
alpha_ij<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_inter[1]
lambda<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_D$Density[x])
pred
})
pred_df_D$Te_mean_inter<-sapply(c(1:length(pred_df_D[,1])), function(x){
alpha_i<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_intra[1]
alpha_ij<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_inter[1]
lambda<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_D$Density[x])
pred
})
pred_df_D$Te_intra_L<-sapply(c(1:length(pred_df_D[,1])), function(x){
alpha_i<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_intra_L[1]
alpha_ij<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_inter_L[1]
lambda<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_lambda_L[1]
pred<-lambda*exp(-alpha_i*pred_df_D$Density[x])
pred
})
pred_df_D$Te_inter_L<-sapply(c(1:length(pred_df_D[,1])), function(x){
alpha_i<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_intra_L[1]
alpha_ij<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_inter_L[1]
lambda<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_lambda_L[1]
pred<-lambda*exp(-alpha_ij*pred_df_D$Density[x])
pred
})
pred_df_D$Te_intra_U<-sapply(c(1:length(pred_df_D[,1])), function(x){
alpha_i<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_intra_U[1]
alpha_ij<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_inter_U[1]
lambda<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_lambda_U[1]
pred<-lambda*exp(-alpha_i*pred_df_D$Density[x])
pred
})
pred_df_D$Te_inter_U<-sapply(c(1:length(pred_df_D[,1])), function(x){
alpha_i<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_intra_U[1]
alpha_ij<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Tu_Regime==pred_df_D$Tu_Regime[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_inter_U[1]
lambda<-subset(alphas_mat_D, Environment==pred_df_D$Environment[x] & Te_Regime==pred_df_D$Te_Regime[x] & Replicate==pred_df_D$Replicate[x])$Te_lambda_U[1]
pred<-lambda*exp(-alpha_ij*pred_df_D$Density[x])
pred
})
# Removing Tu evolved replicate 2 because there is no data
pred_df_D<-pred_df_D[-which(pred_df_D$Tu_Regime==2 & pred_df_D$Replicate==2),]
# Transforming everything bellow 0 into 0 for the lower interval
pred_df_D$Te_inter_L[which(pred_df_D$Te_inter_L<0)]<-0
pred_df_D$Te_intra_L[which(pred_df_D$Te_intra_L<0)]<-0
pred_df_D$Tu_inter_L[which(pred_df_D$Tu_inter_L<0)]<-0
pred_df_D$Tu_intra_L[which(pred_df_D$Tu_intra_L<0)]<-0
ca$Replicate<-as.character(ca$Rep)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_D, Tu_Regime==1 & Environment=="N" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_D, Tu_Regime==1 & Environment=="N" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific control environment")
save_plot("./NewAnalyses/Optim_normal/pred_Tu_INTRA_Control_water.pdf", width=25, height=10)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_D, Tu_Regime==1 & Environment=="C" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_D, Tu_Regime==1 & Environment=="C" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific cadmium environment")
save_plot("./NewAnalyses/Optim_normal/pred_Tu_INTRA_Control_cadmium.pdf", width=25, height=10)
#subset(pred_df_D, Tu_Regime==1 & Environment=="C" & Replicate==3)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_D, Te_Regime==4 & Environment=="N" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_D, Te_Regime==4 & Environment=="N" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="N" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. evansi")+
ggtitle("Te control intraspecific control environment")
save_plot("./NewAnalyses/Optim_normal/pred_Te_INTRA_Control_control.pdf", width=25, height=10)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_D, Te_Regime==4 & Environment=="C" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_D, Te_Regime==4 & Environment=="C" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="Cd" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. evansi")+
ggtitle("Te control intraspecific cadmium environment")
save_plot("./NewAnalyses/Optim_normal/pred_Te_INTRA_Control_cadmium.pdf", width=25, height=10)
# creating folder to put the analyses inside, this should be the same as the file path in the function
dir.create("./NewAnalyses/optim_lambda_fixed", showWarnings = FALSE)
source("./function_riker_27May.R")
# This matrix has all the comparisons that need to be done between regimes
comparison_mat<-matrix(nrow=4, ncol=3)
comparison_mat[1,]<-c(1,4,5)
comparison_mat[2,]<-c(2,4,5)
comparison_mat[3,]<-c(4,1,2)
comparison_mat[4,]<-c(5,1,2)
#lam2 is the data from density one corresponding to the focals populations
# data2 is the data (format) Regime (name of focal pop), background (name of competitor, the same if its intraspecific competition), focal (number of focal individuals in g0), comp (number of competitors in g0), growth rate
# Attention that for intraspecific you need to add 0 in the comp and all individuals in the focal
rep2<-mod_df(subset(ca,Rep==1 & Env=="N"))
magic_rk_lambda(filepath2 = "./NewAnalyses/optim_lambda_fixed/",data2=rep2, reps2=1, env="N", comparisons = comparison_mat, lam2=subset(mean_dens1, Rep==1 & Env=="N"))
[1] "5 model 2 converged on rep 1"
Warning: NaNs producedWarning: NaNs producedWarning: NaNs producedWarning: NaNs producedWarning: NaNs producedWarning: NaNs producedWarning: NaNs produced
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==1 & Env=="Cd"))
magic_rk_lambda(filepath2 = "./NewAnalyses/optim_lambda_fixed/", data2=rep2, reps2=1, env="Cd", comparisons = comparison_mat, lam2=subset(mean_dens1, Rep==1 & Env=="Cd"))
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==3 & Env=="N"))
magic_rk_lambda(filepath2 = "./NewAnalyses/optim_lambda_fixed/", data2=rep2, reps2=3, env="N", comparisons = comparison_mat, lam2=subset(mean_dens1, Rep==3 & Env=="N"))
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==3 & Env=="Cd"))
magic_rk_lambda(filepath2 = "./NewAnalyses/optim_lambda_fixed/", data2=rep2, reps2=3, env="Cd", comparisons = comparison_mat, lam2=subset(mean_dens1, Rep==3 & Env=="Cd"))
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==4 & Env=="N"))
magic_rk_lambda(filepath2 = "./NewAnalyses/optim_lambda_fixed/", data2=rep2, reps2=4, env="N", comparisons = comparison_mat, lam2=subset(mean_dens1, Rep==4 & Env=="N"))
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==4 & Env=="Cd"))
magic_rk_lambda(filepath2 = "./NewAnalyses/optim_lambda_fixed/", data2=rep2, reps2=4, env="Cd", comparisons = comparison_mat, lam2=subset(mean_dens1, Rep==4 & Env=="Cd"))
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==5 & Env=="N"))
magic_rk_lambda(filepath2 = "./NewAnalyses/optim_lambda_fixed/", data2=rep2, reps2=5, env="N", comparisons = comparison_mat, lam2=subset(mean_dens1, Rep==5 & Env=="N"))
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==5 & Env=="Cd"))
magic_rk_lambda(filepath2 = "./NewAnalyses/optim_lambda_fixed/",data2=rep2, reps2=5, env="Cd", comparisons = comparison_mat, lam2=subset(mean_dens1, Rep==5 & Env=="Cd"))
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 2 converged on rep 1"
[1] "2 model 3 converged on rep 1"
[1] "2 model 3 converged on rep 1"
# For two we have to change the comparison matrix
comparison_mat2<-matrix(nrow=3, ncol=3)
comparison_mat2[1,]<-c(1,4,5)
comparison_mat2[2,]<-c(4,1,NA)
comparison_mat2[3,]<-c(5,1,NA)
rep2<-mod_df(subset(ca,Rep==2 & Env=="N"))
magic_rk_lambda(filepath2 = "./NewAnalyses/optim_lambda_fixed/", data2=rep2, reps2=2, env="N", comparisons = comparison_mat2, lam2=subset(mean_dens1, Rep==2 & Env=="N"))
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "1 model 2 converged on rep 1"
[1] "1 model 3 converged on rep 1"
[1] "1 model 2 converged on rep 1"
[1] "1 model 3 converged on rep 1"
rep2<-mod_df(subset(ca,Rep==2 & Env=="Cd"))
magic_rk_lambda(filepath2 = "./NewAnalyses/optim_lambda_fixed/", data2=rep2, reps2=2, env="Cd", comparisons = comparison_mat2, lam2=subset(mean_dens1, Rep==2 & Env=="Cd"))
[1] "5 model 2 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "5 model 3 converged on rep 1"
[1] "1 model 2 converged on rep 1"
[1] "1 model 3 converged on rep 1"
[1] "1 model 2 converged on rep 1"
[1] "1 model 3 converged on rep 1"
alpha_file<-list.files("./NewAnalyses/optim_lambda_fixed/", pattern="alpha_estimates") #the alphas are always tu, te (row), tu, te (col)
alphaUpper_file<-list.files("./NewAnalyses/optim_lambda_fixed/", pattern="alpha_upper")
alphaLower_file<-list.files("./NewAnalyses/optim_lambda_fixed/", pattern="alpha_lower")
lambda_file<-list.files("./NewAnalyses/optim_lambda_fixed/", pattern="lambda_estimates")
alpha_list<- lapply(alpha_file, function(x) read.csv(paste("./NewAnalyses/optim_lambda_fixed/",x, sep=""), header = TRUE))
alphaUpper_list<- lapply(alphaUpper_file, function(x) read.csv(paste("./NewAnalyses/optim_lambda_fixed/",x, sep=""), header = TRUE))
alphaLower_list<- lapply(alphaLower_file, function(x) read.csv(paste("./NewAnalyses/optim_lambda_fixed/",x, sep=""), header = TRUE))
lambda_list<- lapply(lambda_file, function(x) read.csv(paste("./NewAnalyses/optim_lambda_fixed/",x, sep=""), header = TRUE))
# passing from list to data frame
# First we need to do the first iteration (to create everything)
lambda_intra_fixed<-data.frame(Regime1=rep(c(1,1,2,2),10), Regime2=rep(c(4,5,4,5), 10), Replicate=c(rep(1,8),rep(2,8),rep(3,8),rep(4,8),rep(5,8)), Env=rep(c(rep("N",4), rep("Cd",4)), 5))
lambda_intra_fixed<-lambda_intra_fixed[-which(lambda_intra_fixed$Regime1==2 & lambda_intra_fixed$Replicate==2),] # to remove SR2 from replicate 2 because it does not exist
alpha_list[[1]]
lambda_list[[1]]
# passing alphas to dataframe
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[1], split="_")[1])[6], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[1], split="_")[1])[6], split="[.]"))[1],split=""))[2]
regimeTu<-c("1","1", "2","2")
regimeTe<-c("4","5", "4","5")
Env<-rep(env, 4)
Rep<-rep(repli,4)
aux_alpha<-as.data.frame(alpha_list[[1]])
aux_alpha2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu=c(aux_alpha[1,2], aux_alpha[1,2], aux_alpha[2,2],aux_alpha[2,2]), intraTe=c(aux_alpha[3,2], aux_alpha[4,2], aux_alpha[3,2],aux_alpha[4,2]), interTu=c(aux_alpha[1,3], aux_alpha[1,4], aux_alpha[2,3], aux_alpha[2,4]), interTe=c(aux_alpha[3,3], aux_alpha[4,3], aux_alpha[3,4], aux_alpha[4,4]))
for(x in 2:length(lambda_list)){
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[x], split="_")[1])[6], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[x], split="_")[1])[6], split="[.]"))[1],split=""))[2]
if(x==3 | x==4){# because there is no SR2 here
regimeTu<-c("1","1")
regimeTe<-c("4","5")
Env<-rep(env, 2)
Rep<-rep(repli,2)
aux_alpha<-as.data.frame(alpha_list[[x]])
aux2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu=c(aux_alpha[1,2], aux_alpha[1,2]), intraTe=c(aux_alpha[2,2], aux_alpha[3,2]), interTu=c(aux_alpha[1,3], aux_alpha[1,4]), interTe=c(aux_alpha[2,3], aux_alpha[3,3]))
}else{
regimeTu<-c("1","1", "2","2")
regimeTe<-c("4","5", "4","5")
Env<-rep(env, 4)
Rep<-rep(repli,4)
aux_alpha<-as.data.frame(alpha_list[[x]])
aux2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu=c(aux_alpha[1,2], aux_alpha[1,2], aux_alpha[2,2],aux_alpha[2,2]), intraTe=c(aux_alpha[3,2], aux_alpha[4,2], aux_alpha[3,2],aux_alpha[4,2]), interTu=c(aux_alpha[1,3], aux_alpha[1,4], aux_alpha[2,3], aux_alpha[2,4]), interTe=c(aux_alpha[3,3], aux_alpha[4,3], aux_alpha[3,4], aux_alpha[4,4]))
}
aux_alpha2<-rbind(aux_alpha2, aux2)
}
### Alpha Lower
# passing alphas to dataframe
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaLower_file[1], split="_")[1])[4], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaLower_file[1], split="_")[1])[4], split="[.]"))[1],split=""))[2]
regimeTu<-c("1","1", "2","2")
regimeTe<-c("4","5", "4","5")
Env<-rep(env, 4)
Rep<-rep(repli,4)
aux_alphaLower<-as.data.frame(alphaLower_list[[1]])
aux_alphaLower2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu_L=c(aux_alphaLower[1,2], aux_alphaLower[1,2], aux_alphaLower[2,2],aux_alphaLower[2,2]), intraTe_L=c(aux_alphaLower[3,2], aux_alphaLower[4,2], aux_alphaLower[3,2],aux_alphaLower[4,2]), interTu_L=c(aux_alphaLower[1,3], aux_alphaLower[1,4], aux_alphaLower[2,3], aux_alphaLower[2,4]), interTe_L=c(aux_alphaLower[3,3], aux_alphaLower[4,3], aux_alphaLower[3,4], aux_alphaLower[4,4]))
for(x in 2:length(lambda_list)){
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaLower_file[x], split="_")[1])[4], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaLower_file[x], split="_")[1])[4], split="[.]"))[1],split=""))[2]
if(x==3 | x==4){# because there is no SR2 here
regimeTu<-c("1","1")
regimeTe<-c("4","5")
Env<-rep(env, 2)
Rep<-rep(repli,2)
aux_alphaLower<-as.data.frame(alphaLower_list[[x]])
aux2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu_L=c(aux_alphaLower[1,2], aux_alphaLower[1,2]), intraTe_L=c(aux_alphaLower[2,2], aux_alphaLower[3,2]), interTu_L=c(aux_alphaLower[1,3], aux_alphaLower[1,4]), interTe_L=c(aux_alphaLower[2,3], aux_alphaLower[3,3]))
}else{
regimeTu<-c("1","1", "2","2")
regimeTe<-c("4","5", "4","5")
Env<-rep(env, 4)
Rep<-rep(repli,4)
aux_alphaLower<-as.data.frame(alphaLower_list[[x]])
aux2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu_L=c(aux_alphaLower[1,2], aux_alphaLower[1,2], aux_alphaLower[2,2],aux_alphaLower[2,2]), intraTe_L=c(aux_alphaLower[3,2], aux_alphaLower[4,2], aux_alphaLower[3,2],aux_alphaLower[4,2]), interTu_L=c(aux_alphaLower[1,3], aux_alphaLower[1,4], aux_alphaLower[2,3], aux_alphaLower[2,4]), interTe_L=c(aux_alphaLower[3,3], aux_alphaLower[4,3], aux_alphaLower[3,4], aux_alphaLower[4,4]))
}
aux_alphaLower2<-rbind(aux_alphaLower2, aux2)
}
### Alpha Upper
# passing alphas to dataframe
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaUpper_file[1], split="_")[1])[4], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaUpper_file[1], split="_")[1])[4], split="[.]"))[1],split=""))[2]
regimeTu<-c("1","1", "2","2")
regimeTe<-c("4","5", "4","5")
Env<-rep(env, 4)
Rep<-rep(repli,4)
aux_alphaUpper<-as.data.frame(alphaUpper_list[[1]])
aux_alphaUpper2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu_U=c(aux_alphaUpper[1,2], aux_alphaUpper[1,2], aux_alphaUpper[2,2],aux_alphaUpper[2,2]), intraTe_U=c(aux_alphaUpper[3,2], aux_alphaUpper[4,2], aux_alphaUpper[3,2],aux_alphaUpper[4,2]), interTu_U=c(aux_alphaUpper[1,3], aux_alphaUpper[1,4], aux_alphaUpper[2,3], aux_alphaUpper[2,4]), interTe_U=c(aux_alphaUpper[3,3], aux_alphaUpper[4,3], aux_alphaUpper[3,4], aux_alphaUpper[4,4]))
for(x in 2:length(lambda_list)){
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaUpper_file[x], split="_")[1])[4], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alphaUpper_file[x], split="_")[1])[4], split="[.]"))[1],split=""))[2]
if(x==3 | x==4){# because there is no SR2 here
regimeTu<-c("1","1")
regimeTe<-c("4","5")
Env<-rep(env, 2)
Rep<-rep(repli,2)
aux_alphaUpper<-as.data.frame(alphaUpper_list[[x]])
aux2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu_U=c(aux_alphaUpper[1,2], aux_alphaUpper[1,2]), intraTe_U=c(aux_alphaUpper[2,2], aux_alphaUpper[3,2]), interTu_U=c(aux_alphaUpper[1,3], aux_alphaUpper[1,4]), interTe_U=c(aux_alphaUpper[2,3], aux_alphaUpper[3,3]))
}else{
regimeTu<-c("1","1", "2","2")
regimeTe<-c("4","5", "4","5")
Env<-rep(env, 4)
Rep<-rep(repli,4)
aux_alphaUpper<-as.data.frame(alphaUpper_list[[x]])
aux2<-data.frame(regimeTu, regimeTe, Env, Rep, intraTu_U=c(aux_alphaUpper[1,2], aux_alphaUpper[1,2], aux_alphaUpper[2,2],aux_alphaUpper[2,2]), intraTe_U=c(aux_alphaUpper[3,2], aux_alphaUpper[4,2], aux_alphaUpper[3,2],aux_alphaUpper[4,2]), interTu_U=c(aux_alphaUpper[1,3], aux_alphaUpper[1,4], aux_alphaUpper[2,3], aux_alphaUpper[2,4]), interTe_U=c(aux_alphaUpper[3,3], aux_alphaUpper[4,3], aux_alphaUpper[3,4], aux_alphaUpper[4,4]))
}
aux_alphaUpper2<-rbind(aux_alphaUpper2, aux2)
}
# Passing lambda to data frame
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[1], split="_")[1])[6], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[1], split="_")[1])[6], split="[.]"))[1],split=""))[2]
Focal<-c("1","1","2","2","4","4","5","5")
Comp<-c("4","5","4","5","1","2","1","2")
Env<-rep(env, 8)
Rep<-rep(repli,8)
aux_lambda<-cbind(as.data.frame(lambda_list[[1]])[,c(3,4,5)],Focal,Comp, Env, Rep)
for(x in 2:length(lambda_list)){
repli<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[x], split="_")[1])[6], split="[.]"))[1],split=""))[1]
env<-unlist(strsplit(unlist(strsplit(unlist(strsplit(alpha_file[x], split="_")[1])[6], split="[.]"))[1],split=""))[2]
if(x==3 | x==4){# because there is no SR2 here
Focal<-c("1","1","4","5")
Comp<-c("4","5","1","1")
Env<-rep(env, 4)
Rep<-rep(repli,4)
aux<-cbind(as.data.frame(lambda_list[[x]])[,c(3,4,5)],Focal,Comp, Env, Rep)
}else{
Focal<-c("1","1","2","2","4","4","5","5")
Comp<-c("4","5","4","5","1","2","1","2")
Env<-rep(env, 8)
Rep<-rep(repli,8)
aux<-cbind(as.data.frame(lambda_list[[x]])[,c(3,4,5)],Focal,Comp, Env, Rep)
}
aux_lambda<-rbind(aux_lambda, aux)
}
#Matching all the data
alphas_mat_E<-as.data.frame(cbind(aux_alpha2, aux_alphaLower2, aux_alphaUpper2))
str(lambda_intra_fixed)
'data.frame': 36 obs. of 4 variables:
$ Regime1 : num 1 1 2 2 1 1 2 2 1 1 ...
$ Regime2 : num 4 5 4 5 4 5 4 5 4 5 ...
$ Replicate: num 1 1 1 1 1 1 1 1 2 2 ...
$ Env : chr "N" "N" "N" "N" ...
#### adding lambda
alphas_mat_E$lambdaTu<-sapply(c(1:length(alphas_mat_E[,1])), function(x){
auxi<-subset(aux_lambda, (Focal==alphas_mat_E$regimeTu[x] & Comp==alphas_mat_E$regimeTe[x]) & Rep==alphas_mat_E$Rep[x] & Env==alphas_mat_E$Env[x] )
auxi[1,1]
})
alphas_mat_E$lambdaTe<-sapply(c(1:length(alphas_mat_E[,1])), function(x){
auxi<-subset(aux_lambda, (Focal==alphas_mat_E$regimeTe[x] & Comp==alphas_mat_E$regimeTu[x]) & Rep==alphas_mat_E$Rep[x] & Env==alphas_mat_E$Env[x] )
auxi[1,1]
})
alphas_mat_E$lambdaTu_L<-sapply(c(1:length(alphas_mat_E[,1])), function(x){
auxi<-subset(aux_lambda, (Focal==alphas_mat_E$regimeTu[x] & Comp==alphas_mat_E$regimeTe[x]) & Rep==alphas_mat_E$Rep[x] & Env==alphas_mat_E$Env[x] )
auxi[1,2]
})
alphas_mat_E$lambdaTe_L<-sapply(c(1:length(alphas_mat_E[,1])), function(x){
auxi<-subset(aux_lambda, (Focal==alphas_mat_E$regimeTe[x] & Comp==alphas_mat_E$regimeTu[x]) & Rep==alphas_mat_E$Rep[x] & Env==alphas_mat_E$Env[x] )
auxi[1,2]
})
alphas_mat_E$lambdaTu_U<-sapply(c(1:length(alphas_mat_E[,1])), function(x){
auxi<-subset(aux_lambda, (Focal==alphas_mat_E$regimeTu[x] & Comp==alphas_mat_E$regimeTe[x]) & Rep==alphas_mat_E$Rep[x] & Env==alphas_mat_E$Env[x] )
auxi[1,3]
})
alphas_mat_E$lambdaTe_U<-sapply(c(1:length(alphas_mat_E[,1])), function(x){
auxi<-subset(aux_lambda, (Focal==alphas_mat_E$regimeTe[x] & Comp==alphas_mat_E$regimeTu[x]) & Rep==alphas_mat_E$Rep[x] & Env==alphas_mat_E$Env[x] )
auxi[1,3]
})
alphas_mat_E$Env2<-mapvalues(alphas_mat_E$Env, c("C","N"), c("Cd","N"))
# clean up the matrix, because it has a lot of repeated columns
alphas_mat_E<-alphas_mat_E[,c(1:8, 13:16,21:30)]
alphas_mat_E
NA
### Plotting data
colnames(alphas_mat_E)<-c("Tu_Regime", "Te_Regime", "Environment", "Replicate", "Tu_intra", "Te_intra", "Tu_inter", "Te_inter", "Tu_intra_L", "Te_intra_L", "Tu_inter_L", "Te_inter_L", "Tu_intra_U", "Te_intra_U", "Tu_inter_U", "Te_inter_U", "Tu_lambda", "Te_lambda","Tu_lambda_L", "Te_lambda_L","Tu_lambda_U", "Te_lambda_U")
alphas_mat_E_long<-gather(alphas_mat_E, parameter, value,Tu_intra:Te_lambda_U )
alphas_mat_E_long$category<-mapvalues(alphas_mat_E_long$parameter, c("Tu_intra", "Te_intra", "Tu_inter", "Te_inter", "Tu_intra_L", "Te_intra_L", "Tu_inter_L", "Te_inter_L", "Tu_intra_U", "Te_intra_U", "Tu_inter_U", "Te_inter_U", "Tu_lambda", "Te_lambda","Tu_lambda_L", "Te_lambda_L","Tu_lambda_U", "Te_lambda_U"), c("intra", "intra", "inter", "inter", "intra_L", "intra_L", "inter_L", "inter_L","intra_U", "intra_U", "inter_U", "inter_U","lambda","lambda","lambda_L","lambda_L","lambda_U","lambda_U"))
str(alphas_mat_E_long)
'data.frame': 648 obs. of 7 variables:
$ Tu_Regime : chr "1" "1" "2" "2" ...
$ Te_Regime : chr "4" "5" "4" "5" ...
$ Environment: chr "C" "C" "C" "C" ...
$ Replicate : chr "1" "1" "1" "1" ...
$ parameter : chr "Tu_intra" "Tu_intra" "Tu_intra" "Tu_intra" ...
$ value : num 0.043 0.043 0.0684 0.0684 0.1217 ...
$ category : chr "intra" "intra" "intra" "intra" ...
###########
ggplot(subset(alphas_mat_E_long, parameter=="Te_intra" & Tu_Regime=="1" & Environment=="C"), aes(x=Te_Regime, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Environment")
save_plot("./NewAnalyses/optim_lambda_fixed/26May_RK_intraTe_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(alphas_mat_E_long, parameter=="Te_intra" & Tu_Regime=="1" & Environment=="N"), aes(x=Te_Regime, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. evansi)")+
xlab("Environment")
save_plot("./NewAnalyses/optim_lambda_fixed/26May_RK_intraTe_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(alphas_mat_E_long, parameter=="Tu_intra" & Te_Regime=="4" & Environment=="C"), aes(fill=Tu_Regime, y=value, x=Tu_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Environment")
save_plot("./NewAnalyses/optim_lambda_fixed/26May_RK_intraTu_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(alphas_mat_E_long, parameter=="Tu_intra" & Te_Regime=="4" & Environment=="N"), aes(fill=Tu_Regime, y=value, x=Tu_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intraspecific competition (T. urticae)")+
xlab("Environment")
save_plot("./NewAnalyses/optim_lambda_fixed/26May_RK_intraTu_w0_rep_N.pdf", width=20, height=15)
######## INTER
ggplot(subset(alphas_mat_E_long, parameter=="Te_inter" & Environment=="C"), aes(x=Te_Regime, y=value, fill=Te_Regime))+
facet_grid(.~Tu_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_x_discrete(labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. evansi)")+
xlab("Selection regime T. evansi")+
ggtitle("Environment: cadmium")
save_plot("./NewAnalyses/optim_lambda_fixed/26May_RK_interTe_w0_rep_Cd.pdf", width=20, height=15)
ggplot(subset(alphas_mat_E_long, parameter=="Te_inter" & Environment=="N"), aes(x=Te_Regime, y=value, fill=Te_Regime))+
facet_grid(.~Tu_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_x_discrete(labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. evansi)")+
xlab("Selection regime T. evansi")+
ggtitle("Environment: control")
save_plot("./NewAnalyses/optim_lambda_fixed/26May_RK_interTe_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(alphas_mat_E_long, parameter=="Tu_inter" & Environment=="C"), aes(fill=Tu_Regime, y=value, x=Tu_Regime))+
facet_grid(.~Te_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot(outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.75), aes(shape=Replicate))+
theme_ines+
scale_x_discrete(labels=c("Control", "Evolved"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. urticae)")+
xlab("Selection regime T. urticae")+
ggtitle("Environment: cadmium")
save_plot("./NewAnalyses/optim_lambda_fixed/26May_RK_interTu_w0_rep_Cd.pdf", width=20, height=15)
ggplot(subset(alphas_mat_E_long, parameter=="Tu_inter" & Environment=="N"), aes(fill=Tu_Regime, y=value, x=Tu_Regime))+
facet_grid(.~Te_Regime, labeller=labeller(Tu_Regime=regimeTu, Te_Regime=regimeTe))+
geom_hline(yintercept = 0, colour="darkgray")+
geom_boxplot(outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.75), aes(shape=Replicate))+
theme_ines+
scale_x_discrete(labels=c("Control", "Evolved"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Interspecific competition (T. urticae)")+
xlab("Selection regime T. urticae")+
ggtitle("Environment: control")
save_plot("./NewAnalyses/optim_lambda_fixed/26May_RK_interTu_w0_rep_N.pdf", width=20, height=15)
######### Lambda
ggplot(subset(alphas_mat_E_long, parameter=="Te_lambda" & Tu_Regime=="1" & Environment=="C"), aes(x=Te_Regime, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. evansi)")+
xlab("Selection regime T. evansi")+
ggtitle("Environment: cadmium")
save_plot("./NewAnalyses/optim_lambda_fixed/26May_RK_lambdaTe_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(alphas_mat_E_long, parameter=="Te_lambda" & Tu_Regime=="1" & Environment=="N"), aes(x=Te_Regime, y=value, fill=Te_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point(position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
scale_colour_manual(c("#FFCC99", "#D55E30"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. evansi)")+
xlab("Selection regime T. evansi")+
ggtitle("Environment: control")
save_plot("./NewAnalyses/optim_lambda_fixed/26May_RK_lambdaTe_w0_rep_N.pdf", width=20, height=15)
ggplot(subset(alphas_mat_E_long, parameter=="Tu_lambda" & Te_Regime=="4" & Environment=="C"), aes(fill=Tu_Regime, y=value, x=Tu_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. urticae)")+
xlab("Selection regime T. urticae")+
ggtitle("Environment: cadmium")
save_plot("./NewAnalyses/optim_lambda_fixed/26May_RK_lambdaTu_w0_rep_CD.pdf", width=20, height=15)
ggplot(subset(alphas_mat_E_long, parameter=="Tu_lambda" & Te_Regime=="4" & Environment=="N"), aes(fill=Tu_Regime, y=value, x=Tu_Regime))+
geom_boxplot( outlier.alpha = 0.01)+
geom_point( position=position_dodge2(0.5), aes(shape=Replicate))+
theme_ines+
#scale_x_discrete(labels=c("No cadmium", "Cadmium"))+
scale_fill_manual(values=c("#99FF99", "#009E73"),labels=c("Control", "Evolved"))+
scale_colour_manual(values=c("#99FF99", "#009E73"), labels=c("Control", "Evolved"))+
ylab("Intrinsic growth rate (T. urticae)")+
xlab("Selection regime T. urticae")+
ggtitle("Environment: control")
save_plot("./NewAnalyses/optim_lambda_fixed/26May_RK_lambdaTu_w0_rep_N.pdf", width=20, height=15)
NA
NA
Predicting data
str(alphas_mat_E)
'data.frame': 36 obs. of 22 variables:
$ Tu_Regime : chr "1" "1" "2" "2" ...
$ Te_Regime : chr "4" "5" "4" "5" ...
$ Environment: chr "C" "C" "C" "C" ...
$ Replicate : chr "1" "1" "1" "1" ...
$ Tu_intra : num 0.043 0.043 0.0684 0.0684 0.1217 ...
$ Te_intra : num 0.103 0.16 0.103 0.16 0.165 ...
$ Tu_inter : num 0.0624 0.0501 0.0503 0.0564 0.0793 ...
$ Te_inter : num 0.0538 0.14 0.0847 0.1147 0.2086 ...
$ Tu_intra_L : num 0.0132 0.0132 0.0362 0.0362 0.0847 ...
$ Te_intra_L : num 0.0701 0.1099 0.0701 0.1099 0.1266 ...
$ Tu_inter_L : num 0.0393 0.0255 0.0183 0.0228 0.0128 ...
$ Te_inter_L : num -0.0114 0.0928 0.029 0.0541 0.1285 ...
$ Tu_intra_U : num 0.0728 0.0728 0.1006 0.1006 0.1587 ...
$ Te_intra_U : num 0.136 0.21 0.136 0.21 0.204 ...
$ Tu_inter_U : num 0.0856 0.0747 0.0823 0.09 0.1458 ...
$ Te_inter_U : num 0.119 0.187 0.14 0.175 0.289 ...
$ Tu_lambda : num 1.6 1.6 1.89 1.89 3.9 ...
$ Te_lambda : num 3.3 5.17 3.3 5.17 11.11 ...
$ Tu_lambda_L: num -0.294 -0.294 -2.085 -2.085 -2.329 ...
$ Te_lambda_L: num -0.402 -7.439 -0.402 -7.439 1.895 ...
$ Tu_lambda_U: num 3.49 3.49 5.86 5.86 10.13 ...
$ Te_lambda_U: num 7 17.8 7 17.8 20.3 ...
alphas_mat_E$Env2<-mapvalues(alphas_mat_E$Environment, c("C", "N"), c("Cd","N"))
str(ca)
'data.frame': 3451 obs. of 45 variables:
$ Block : int 1 1 1 1 1 1 1 1 1 1 ...
$ Rep : int 1 1 1 1 1 1 1 1 1 1 ...
$ Box : int 1 1 1 1 1 1 1 1 1 1 ...
$ Leaf : int 3 4 3 4 3 4 3 4 3 4 ...
$ Disk : int 1 2 3 4 5 6 7 8 9 10 ...
$ Env : chr "N" "N" "Cd" "Cd" ...
$ FocalSR : int 4 4 4 4 4 4 4 4 4 4 ...
$ CompSR : int NA NA NA NA NA NA NA NA NA NA ...
$ Dens : int 1 1 1 1 2 2 2 2 4 4 ...
$ Type : chr "INTRA" "INTRA" "INTRA" "INTRA" ...
$ Focalfemale : chr "Te" "Te" "Te" "Te" ...
$ FocalDead : int 0 0 1 0 1 1 0 0 0 0 ...
$ FocalDrowned : int 0 0 0 0 0 0 0 0 0 0 ...
$ FocalMissing : int 0 0 0 0 0 0 0 0 0 0 ...
$ NumbDeadComp : int 0 0 0 0 0 0 0 0 0 0 ...
$ NumberOfEggs : int 11 21 3 9 15 16 17 11 58 24 ...
$ NumberOfEggsBelow : int 0 0 0 0 0 0 0 0 0 0 ...
$ TeMales : int 3 3 0 3 2 5 5 3 8 8 ...
$ TeFemales : int 6 9 2 5 2 10 4 4 9 6 ...
$ TeQuiescentfemales : int 0 0 0 0 0 0 3 0 3 4 ...
$ TuMales : int NA NA NA NA NA NA NA NA NA NA ...
$ TuFemales : int NA NA NA NA NA NA NA NA NA NA ...
$ TuQuiescentfemales : int NA NA NA NA NA NA NA NA NA NA ...
$ Observations : chr NA NA NA NA ...
$ Block2 : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
$ Rep2 : Factor w/ 5 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Disk2 : Factor w/ 16 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
$ Leaf2 : Factor w/ 2 levels "3","4": 1 2 1 2 1 2 1 2 1 2 ...
$ Env2 : Factor w/ 2 levels "Cd","N": 2 2 1 1 2 2 1 1 2 2 ...
$ FocalSR2 : Factor w/ 4 levels "1","2","4","5": 3 3 3 3 3 3 3 3 3 3 ...
$ CompSR2 : Factor w/ 4 levels "1","2","4","5": NA NA NA NA NA NA NA NA NA NA ...
$ Type2 : Factor w/ 2 levels "INTER","INTRA": 2 2 2 2 2 2 2 2 2 2 ...
$ Focal_Female2 : Factor w/ 2 levels "Te","Tu": 1 1 1 1 1 1 1 1 1 1 ...
$ Nr_Focal_Females_Tu_Alive_G0: num NA NA NA NA NA NA NA NA NA NA ...
$ Nr_Focal_Females_Te_Alive_G0: num 1 1 0 1 1 1 2 2 4 4 ...
$ Num_Comp_Tu_Alive_G0 : num NA NA NA NA NA NA NA NA NA NA ...
$ Num_Comp_Te_Alive_G0 : num NA NA NA NA NA NA NA NA NA NA ...
$ Nr_Focal_Females_G0 : num 1 1 1 1 2 2 2 2 4 4 ...
$ Nr_Comp_Females_G0 : num 0 0 0 0 1 1 1 1 3 3 ...
$ GrowthRateOA : num 6 9 2 5 1 5 2 2 2.25 1.5 ...
$ GrowthRateOA_Q : num 6 9 2 5 1 5 3.5 2 3 2.5 ...
$ Replicate : chr "1" "1" "1" "1" ...
$ pred_D : num 0.131 0.131 0.072 0.072 0.113 ...
$ pred_D_L : num 7.34 7.34 1.68 1.68 6.54 ...
$ pred_D_U : num 13.05 13.05 3.84 3.84 10.79 ...
# Since the lambda is from the log data
ca$pred_E<-sapply(c(1:length(ca$Block)), function(x){
if(ca$Focalfemale[x]=="Tu"){
alpha_i<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Tu_Regime==as.character(ca$FocalSR[x]) & Replicate==ca$Rep[x])$Tu_intra[1]
alpha_ij<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Te_Regime==ca$CompSR[x] & Replicate==ca$Rep[x])$Tu_inter[1]
lambda<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Tu_lambda[1]
}else if(ca$Focalfemale[x]=="Te"){
alpha_i<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Te_intra[1]
alpha_ij<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Tu_Regime==ca$CompSR[x] & Replicate==ca$Rep[x])$Te_inter[1]
lambda<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Te_lambda[1]
}
if(ca$Type[x]=="INTRA"){
densF<-ca$Dens[x]
pred<-lambda*exp(-alpha_i*densF)
}else if(ca$Type[x]=="INTER"){
densC<-ca$Dens[x]
pred<-lambda*exp(-alpha_ij*densC)
}
pred
})
x<-1
ca$pred_E_L<-sapply(c(1:length(ca$Block)), function(x){
if(ca$Focalfemale[x]=="Tu"){
alpha_i<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Tu_intra_L[1]
alpha_ij<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Te_Regime==ca$CompSR[x] & Replicate==ca$Rep[x])$Tu_inter_L[1]
lambda<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Tu_lambda_L[1]
}else if(ca$Focalfemale[x]=="Te"){
alpha_i<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Te_intra_L[1]
alpha_ij<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Tu_Regime==ca$CompSR[x] & Replicate==ca$Rep[x])$Te_inter_L[1]
lambda<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Te_lambda_L[1]
}
if(ca$Type[x]=="INTRA"){
densF<-ca$Dens[x]
pred<-lambda*exp(-alpha_i*densF)
}else if(ca$Type[x]=="INTER"){
densC<-ca$Dens[x]-1
pred<-lambda*exp(-alpha_ij*densC)
}
pred
})
ca$pred_E_U<-sapply(c(1:length(ca$Block)), function(x){
if(ca$Focalfemale[x]=="Tu"){
alpha_i<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Tu_intra_U[1]
alpha_ij<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Te_Regime==ca$CompSR[x] & Replicate==ca$Rep[x])$Tu_inter_U[1]
lambda<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Tu_lambda_U[1]
}else if(ca$Focalfemale[x]=="Te"){
alpha_i<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Te_intra_U[1]
alpha_ij<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Tu_Regime==ca$CompSR[x] & Replicate==ca$Rep[x])$Te_inter_U[1]
lambda<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR[x] & Replicate==ca$Rep[x])$Te_lambda_U[1]
}
if(ca$Type[x]=="INTRA"){
densF<-ca$Dens[x]
pred<-lambda*exp(-alpha_i*densF)
}else if(ca$Type[x]=="INTER"){
densC<-ca$Dens[x]-1
pred<-lambda*exp(-alpha_ij*densC)
}
pred
})
density_aux<-seq(0, 10, by=(10/100))
pred_df_E<-as.data.frame(expand_grid(Density=density_aux, Tu_Regime=c(1,2), Te_Regime=c(4,5), Replicate=c(1:5), Environment=c("N", "C")))
pred_df_E$Tu_mean_intra<-sapply(c(1:length(pred_df_E[,1])), function(x){
alpha_i<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_inter[1]
lambda<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_E$Density[x])
pred
})
pred_df_E$Tu_mean_inter<-sapply(c(1:length(pred_df_E[,1])), function(x){
alpha_i<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_intra[1]
alpha_ij<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_inter[1]
lambda<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_E$Density[x])
pred
})
pred_df_E$Tu_intra_L<-sapply(c(1:length(pred_df_E[,1])), function(x){
alpha_i<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_intra_L[1]
alpha_ij<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_inter_L[1]
lambda<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_lambda_L[1]
pred<-lambda*exp(-alpha_i*pred_df_E$Density[x])
pred
})
pred_df_E$Tu_inter_L<-sapply(c(1:length(pred_df_E[,1])), function(x){
alpha_i<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_intra_L[1]
alpha_ij<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_inter_L[1]
lambda<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_lambda_L[1]
pred<-lambda*exp(-alpha_ij*pred_df_E$Density[x])
pred
})
pred_df_E$Tu_intra_U<-sapply(c(1:length(pred_df_E[,1])), function(x){
alpha_i<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_intra_U[1]
alpha_ij<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_inter_U[1]
lambda<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_lambda_U[1]
pred<-lambda*exp(-alpha_i*pred_df_E$Density[x])
pred
})
pred_df_E$Tu_inter_U<-sapply(c(1:length(pred_df_E[,1])), function(x){
alpha_i<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_intra_U[1]
alpha_ij<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_inter_U[1]
lambda<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Replicate==pred_df_E$Replicate[x])$Tu_lambda_U[1]
pred<-lambda*exp(-alpha_ij*pred_df_E$Density[x])
pred
})
pred_df_E$Te_mean_intra<-sapply(c(1:length(pred_df_E[,1])), function(x){
alpha_i<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_intra[1]
alpha_ij<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_inter[1]
lambda<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_i*pred_df_E$Density[x])
pred
})
pred_df_E$Te_mean_inter<-sapply(c(1:length(pred_df_E[,1])), function(x){
alpha_i<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_intra[1]
alpha_ij<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_inter[1]
lambda<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_lambda[1]
pred<-lambda*exp(-alpha_ij*pred_df_E$Density[x])
pred
})
pred_df_E$Te_intra_L<-sapply(c(1:length(pred_df_E[,1])), function(x){
alpha_i<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_intra_L[1]
alpha_ij<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_inter_L[1]
lambda<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_lambda_L[1]
pred<-lambda*exp(-alpha_i*pred_df_E$Density[x])
pred
})
pred_df_E$Te_inter_L<-sapply(c(1:length(pred_df_E[,1])), function(x){
alpha_i<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_intra_L[1]
alpha_ij<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_inter_L[1]
lambda<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_lambda_L[1]
pred<-lambda*exp(-alpha_ij*pred_df_E$Density[x])
pred
})
pred_df_E$Te_intra_U<-sapply(c(1:length(pred_df_E[,1])), function(x){
alpha_i<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_intra_U[1]
alpha_ij<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_inter_U[1]
lambda<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_lambda_U[1]
pred<-lambda*exp(-alpha_i*pred_df_E$Density[x])
pred
})
pred_df_E$Te_inter_U<-sapply(c(1:length(pred_df_E[,1])), function(x){
alpha_i<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_intra_U[1]
alpha_ij<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Tu_Regime==pred_df_E$Tu_Regime[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_inter_U[1]
lambda<-subset(alphas_mat_E, Environment==pred_df_E$Environment[x] & Te_Regime==pred_df_E$Te_Regime[x] & Replicate==pred_df_E$Replicate[x])$Te_lambda_U[1]
pred<-lambda*exp(-alpha_ij*pred_df_E$Density[x])
pred
})
# Removing Tu evolved replicate 2 because there is no data
pred_df_E<-pred_df_E[-which(pred_df_E$Tu_Regime==2 & pred_df_E$Replicate==2),]
# Transforming everything bellow 0 into 0 for the lower interval
pred_df_E$Te_inter_L[which(pred_df_E$Te_inter_L<0)]<-0
pred_df_E$Te_intra_L[which(pred_df_E$Te_intra_L<0)]<-0
pred_df_E$Tu_inter_L[which(pred_df_E$Tu_inter_L<0)]<-0
pred_df_E$Tu_intra_L[which(pred_df_E$Tu_intra_L<0)]<-0
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Tu_Regime==1 & Environment=="N" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Tu_Regime==1 & Environment=="N" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific control environment")
ca$Replicate<-as.character(ca$Rep)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Tu_Regime==1 & Environment=="N" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Tu_Regime==1 & Environment=="N" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific control environment")
save_plot("./NewAnalyses/optim_lambda_fixed/pred_Tu_INTRA_Control_water.pdf", width=25, height=10)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Tu_Regime==1 & Environment=="C" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Tu_Regime==1 & Environment=="C" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. urticae")+
ggtitle("Tu control intraspecific cadmium environment")
save_plot("./NewAnalyses/optim_lambda_fixed/pred_Tu_INTRA_Control_cadmium.pdf", width=25, height=10)
#subset(pred_df_E, Tu_Regime==1 & Environment=="C" & Replicate==3)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Te_Regime==4 & Environment=="N" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Te_Regime==4 & Environment=="N" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="N" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. evansi")+
ggtitle("Te control intraspecific control environment")
save_plot("./NewAnalyses/optim_lambda_fixed/pred_Te_INTRA_Control_control.pdf", width=25, height=10)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Te_Regime==4 & Environment=="C" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Te_Regime==4 & Environment=="C" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="Cd" & Type=="INTRA"), aes(x=Dens-1, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ylab("Intraspecific competition T. evansi")+
ggtitle("Te control intraspecific cadmium environment")
save_plot("./NewAnalyses/optim_lambda_fixed/pred_Te_INTRA_Control_cadmium.pdf", width=25, height=10)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Tu_Regime==1 & Environment=="N" & Te_Regime==4), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Tu_Regime==1 & Environment=="N" & Te_Regime==4), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu & Te control interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Tu_Regime==1 & Environment=="C" & Te_Regime==4), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Tu_Regime==1 & Environment=="C" & Te_Regime==4), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu & Te control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Te_Regime==4 & Environment=="N" & Tu_Regime==1), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Te_Regime==4 & Environment=="N" & Tu_Regime==1), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="N" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te & Tu control intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Te_Regime==4 & Environment=="C" & Tu_Regime==1), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Te_Regime==4 & Environment=="C" & Tu_Regime==1), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="Cd" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te & Tu control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Tu_Regime==1 & Environment=="N" & Te_Regime==5), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Tu_Regime==1 & Environment=="N" & Te_Regime==5), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="N" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu control & Te evolved interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Tu_Regime==1 & Environment=="C" & Te_Regime==5), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Tu_Regime==1 & Environment=="C" & Te_Regime==5), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==1 & Env=="Cd" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu control & Te evolved interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Te_Regime==4 & Environment=="N" & Tu_Regime==2), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Te_Regime==4 & Environment=="N" & Tu_Regime==2), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="N" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te control & Tu evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Te_Regime==4 & Environment=="C" & Tu_Regime==2), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Te_Regime==4 & Environment=="C" & Tu_Regime==2), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==4 & Env=="Cd" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te control & Tu evolved interspecific cadmium environment")
ca$Replicate<-as.character(ca$Rep)
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Tu_Regime==2 & Environment=="N" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Tu_Regime==2 & Environment=="N" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="N" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Tu_Regime==2 & Environment=="C" ), aes(x=Density, ymin=Tu_intra_L, ymax=Tu_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Tu_Regime==2 & Environment=="C" ), aes(x=Density, y=Tu_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="Cd" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved intraspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Te_Regime==5 & Environment=="N" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Te_Regime==5 & Environment=="N" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="N" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Te_Regime==5 & Environment=="C" ), aes(x=Density, ymin=Te_intra_L, ymax=Te_intra_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Te_Regime==5 & Environment=="C" ), aes(x=Density, y=Te_mean_intra), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="Cd" & Type=="INTRA"), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved intraspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Tu_Regime==2 & Environment=="N" & Te_Regime==4), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Tu_Regime==2 & Environment=="N" & Te_Regime==4), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="N" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te control interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Tu_Regime==2 & Environment=="C" & Te_Regime==4), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Tu_Regime==2 & Environment=="C" & Te_Regime==4), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="Cd" & Type=="INTER" & CompSR==4), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Te_Regime==5 & Environment=="N" & Tu_Regime==1), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Te_Regime==5 & Environment=="N" & Tu_Regime==1), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="N" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu control intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Te_Regime==5 & Environment=="C" & Tu_Regime==1), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Te_Regime==5 & Environment=="C" & Tu_Regime==1), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="Cd" & Type=="INTER" & CompSR==1), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu control interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Tu_Regime==2 & Environment=="N" & Te_Regime==5), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Tu_Regime==2 & Environment=="N" & Te_Regime==5), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="N" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te evolved interspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Tu_Regime==2 & Environment=="C" & Te_Regime==5), aes(x=Density, ymin=Tu_inter_L, ymax=Tu_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Tu_Regime==2 & Environment=="C" & Te_Regime==5), aes(x=Density, y=Tu_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==2 & Env=="Cd" & Type=="INTER" & CompSR==5), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Tu evolved & Te evolved interspecific cadmium environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Te_Regime==5 & Environment=="N" & Tu_Regime==2), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Te_Regime==5 & Environment=="N" & Tu_Regime==2), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="N" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu evolved intraspecific control environment")
ggplot()+
facet_grid(.~Replicate)+
geom_ribbon(data=subset(pred_df_E, Te_Regime==5 & Environment=="C" & Tu_Regime==2), aes(x=Density, ymin=Te_inter_L, ymax=Te_inter_U), alpha=0.85, colour="black", fill="lightgray", linetype="dashed")+
geom_line(data=subset(pred_df_E, Te_Regime==5 & Environment=="C" & Tu_Regime==2), aes(x=Density, y=Te_mean_inter), colour="black")+
geom_point(data=subset(ca, FocalSR==5 & Env=="Cd" & Type=="INTER" & CompSR==2), aes(x=Dens, y=GrowthRateOA))+
theme_bw()+
theme_ines+
ggtitle("Te evolved & Tu evolved interspecific cadmium environment")
# Putting names of selection regimes all the same
alphas_mat_D$Tu_Regime2<-alphas_mat_D$Tu_Regime
alphas_mat_D$Te_Regime2<-alphas_mat_D$Te_Regime
alphas_mat_D$Tu_Regime<-mapvalues(alphas_mat_D$Tu_Regime2, c("1","2","4","5"), c("SR1", "SR2", "SR4","SR5"))
The following `from` values were not present in `x`: 4, 5
alphas_mat_D$Te_Regime<-mapvalues(alphas_mat_D$Te_Regime2, c("1","2","4","5"), c("SR1", "SR2", "SR4","SR5"))
The following `from` values were not present in `x`: 1, 2
alphas_mat_E$Tu_Regime2<-alphas_mat_E$Tu_Regime
alphas_mat_E$Te_Regime2<-alphas_mat_E$Te_Regime
alphas_mat_E$Tu_Regime<-mapvalues(alphas_mat_E$Tu_Regime2, c("1","2","4","5"), c("SR1", "SR2", "SR4","SR5"))
The following `from` values were not present in `x`: 4, 5
alphas_mat_E$Te_Regime<-mapvalues(alphas_mat_E$Te_Regime2, c("1","2","4","5"), c("SR1", "SR2", "SR4","SR5"))
The following `from` values were not present in `x`: 1, 2
# doing the same with the environments
alphas_mat_D$Environment2<-alphas_mat_D$Environment
alphas_mat_D$Environment<-mapvalues(alphas_mat_D$Environment2, c("N","C"), c("N", "Cd"))
alphas_mat_E$Environment2<-alphas_mat_E$Environment
alphas_mat_E$Environment<-mapvalues(alphas_mat_E$Environment2, c("N","C"), c("N", "Cd"))
### Adding variable to say who estimated what
param_all_w0$Method<-"cxr"
param_all_B$Method<-"cxr lambda fixed"
param_all_C$Method<-"cxr lambda fixed, nested"
alphas_mat_D$Method<-"optim"
alphas_mat_E$Method<-"optim lambda fixed"
cols_to_join<-c("Tu_Regime", "Te_Regime", "Environment", "Replicate","Tu_lambda","Te_lambda", "Tu_intra","Te_intra", "Tu_inter", "Te_inter", "Method" )
comparison_methods<-rbind(param_all_w0[,cols_to_join],param_all_B[,cols_to_join],param_all_C[,cols_to_join], alphas_mat_D[,cols_to_join], alphas_mat_E[,cols_to_join] )
ggplot(comparison_methods, aes(x=Method, y=Tu_lambda, colour=Environment, fill=Environment, shape=Replicate))+
facet_grid(Tu_Regime~Te_Regime)+
geom_boxplot(aes(group=Method, fill=Environment), alpha=0.75, outlier.colour = NA)+
geom_point(position = position_dodge2(0.5))+
theme_ines+
theme_bw()+
xlab("Methods used to estimate data")+
ylab("Tu lambda")+
scale_x_discrete(labels=c("cxr", "cxr\nlambda","cxr\nnested", "optim", "optim\nlambda"))+
scale_colour_manual(values=c("darkblue", "darkred"))
ggplot(comparison_methods, aes(x=Method, y=Te_lambda, colour=Environment, fill=Environment, shape=Replicate))+
facet_grid(Tu_Regime~Te_Regime)+
geom_boxplot(aes(group=Method, fill=Environment), alpha=0.75, outlier.colour = NA)+
geom_point(position = position_dodge2(0.5))+
theme_ines+
theme_bw()+
xlab("Methods used to estimate data")+
ylab("Te lambda")+
scale_x_discrete(labels=c("cxr", "cxr\nlambda","cxr\nnested", "optim", "optim\nlambda"))+
scale_colour_manual(values=c("darkblue", "darkred"))
ggplot(comparison_methods, aes(x=Method, y=Tu_intra, colour=Environment, fill=Environment, shape=Replicate))+
facet_grid(Tu_Regime~Te_Regime)+
geom_boxplot(aes(group=Method, fill=Environment), alpha=0.75, outlier.colour = NA)+
geom_point(position = position_dodge2(0.5))+
theme_ines+
theme_bw()+
xlab("Methods used to estimate data")+
ylab("Tu intra")+
scale_x_discrete(labels=c("cxr", "cxr\nlambda","cxr\nnested", "optim", "optim\nlambda"))+
scale_colour_manual(values=c("darkblue", "darkred"))
ggplot(comparison_methods, aes(x=Method, y=Te_intra, colour=Environment, fill=Environment, shape=Replicate))+
facet_grid(Tu_Regime~Te_Regime)+
geom_boxplot(aes(group=Method, fill=Environment), alpha=0.75, outlier.colour = NA)+
geom_point(position = position_dodge2(0.5))+
theme_ines+
theme_bw()+
xlab("Methods used to estimate data")+
ylab("Te intra")+
scale_x_discrete(labels=c("cxr", "cxr\nlambda","cxr\nnested", "optim", "optim\nlambda"))+
scale_colour_manual(values=c("darkblue", "darkred"))
ggplot(comparison_methods, aes(x=Method, y=Tu_inter, colour=Environment, fill=Environment, shape=Replicate))+
facet_grid(Tu_Regime~Te_Regime)+
geom_boxplot(aes(group=Method, fill=Environment), alpha=0.75, outlier.colour = NA)+
geom_point(position = position_dodge2(0.5))+
theme_ines+
theme_bw()+
xlab("Methods used to estimate data")+
ylab("Tu inter")+
scale_x_discrete(labels=c("cxr", "cxr\nlambda","cxr\nnested", "optim", "optim\nlambda"))+
scale_colour_manual(values=c("darkblue", "darkred"))
ggplot(comparison_methods, aes(x=Method, y=Te_inter, colour=Environment, fill=Environment, shape=Replicate))+
facet_grid(Tu_Regime~Te_Regime)+
geom_boxplot(aes(group=Method, fill=Environment), alpha=0.75, outlier.colour = NA)+
geom_point(position = position_dodge2(0.5))+
theme_ines+
theme_bw()+
xlab("Methods used to estimate data")+
ylab("Te inter")+
scale_x_discrete(labels=c("cxr", "cxr\nlambda","cxr\nnested", "optim", "optim\nlambda"))+
scale_colour_manual(values=c("darkblue", "darkred"))
Since I can’t really know what is the best approach, I will estimate the predicted vs observed for each method and use that as metric to define which method to use in the results
ca$FocalSR3<-mapvalues(ca$FocalSR, c(1,2,4,5), c("SR1", "SR2","SR4","SR5"))
ca$CompSR3<-mapvalues(ca$CompSR, c(1,2,4,5), c("SR1", "SR2","SR4","SR5"))
ca$Env3<-mapvalues(ca$Env, c("N", "Cd"), c("N", "C"))
# Since the lambda is from the log data
ca$pred_A<-sapply(c(1:length(ca$Block)), function(x){
if(ca$Focalfemale[x]=="Tu"){
alpha_i<-subset(param_all_w0, Environment==as.character(ca$Env[x]) & Tu_Regime==as.character(ca$FocalSR3[x]) & Replicate==ca$Rep[x])$Tu_intra[1]
alpha_ij<-subset(param_all_w0, Environment==as.character(ca$Env[x]) & Tu_Regime==as.character(ca$FocalSR3[x]) & Te_Regime==as.character(ca$CompSR3[x]) & Replicate==ca$Rep[x])$Tu_inter[1]
lambda<-subset(param_all_w0, Environment==as.character(ca$Env[x]) & Tu_Regime==as.character(ca$FocalSR3[x]) & Replicate==ca$Rep[x])$Tu_lambda[1]
}else if(ca$Focalfemale[x]=="Te"){
alpha_i<-subset(param_all_w0, Environment==as.character(ca$Env[x]) & Te_Regime==as.character(ca$FocalSR3[x]) & Replicate==ca$Rep[x])$Te_intra[1]
alpha_ij<-subset(param_all_w0, Environment==as.character(ca$Env[x]) & Te_Regime==as.character(ca$FocalSR3[x]) & Tu_Regime==as.character(ca$CompSR3[x]) & Replicate==ca$Rep[x])$Te_inter[1]
lambda<-subset(param_all_w0, Environment==as.character(ca$Env[x]) & Te_Regime==as.character(ca$FocalSR3[x]) & Replicate==ca$Rep[x])$Te_lambda[1]
}
if(ca$Type[x]=="INTRA"){
densF<-ca$Dens[x]
pred<-lambda*exp(-alpha_i*(densF))
}else if(ca$Type[x]=="INTER"){
densC<-ca$Dens[x]-1
densF<-1
pred<-lambda*exp(-alpha_i*(densF)-alpha_ij*densC)
}
pred
})
ca$pred_B<-sapply(c(1:length(ca$Block)), function(x){
if(ca$Focalfemale[x]=="Tu"){
alpha_i<-subset(param_all_B, Environment==as.character(ca$Env[x]) & Tu_Regime==as.character(ca$FocalSR3[x]) & Replicate==ca$Rep[x])$Tu_intra[1]
alpha_ij<-subset(param_all_B, Environment==as.character(ca$Env[x]) & Tu_Regime==as.character(ca$FocalSR3[x]) & Te_Regime==as.character(ca$CompSR3[x]) & Replicate==ca$Rep[x])$Tu_inter[1]
lambda<-subset(param_all_B, Environment==as.character(ca$Env[x]) & Tu_Regime==as.character(ca$FocalSR3[x]) & Replicate==ca$Rep[x])$Tu_lambda[1]
}else if(ca$Focalfemale[x]=="Te"){
alpha_i<-subset(param_all_B, Environment==as.character(ca$Env[x]) & Te_Regime==as.character(ca$FocalSR3[x]) & Replicate==ca$Rep[x])$Te_intra[1]
alpha_ij<-subset(param_all_B, Environment==as.character(ca$Env[x]) & Te_Regime==as.character(ca$FocalSR3[x]) & Tu_Regime==as.character(ca$CompSR3[x]) & Replicate==ca$Rep[x])$Te_inter[1]
lambda<-subset(param_all_B, Environment==as.character(ca$Env[x]) & Te_Regime==as.character(ca$FocalSR3[x]) & Replicate==ca$Rep[x])$Te_lambda[1]
}
if(ca$Type[x]=="INTRA"){
densF<-ca$Dens[x]
pred<-lambda*exp(-alpha_i*(densF))
}else if(ca$Type[x]=="INTER"){
densC<-ca$Dens[x]-1
densF<-1
pred<-lambda*exp(-alpha_i*(densF)-alpha_ij*densC)
}
pred
})
ca$pred_C<-sapply(c(1:length(ca$Block)), function(x){
if(ca$Focalfemale[x]=="Tu"){
alpha_i<-subset(param_all_C, Environment==as.character(ca$Env[x]) & Tu_Regime==as.character(ca$FocalSR3[x]) & Replicate==ca$Rep[x])$Tu_intra[1]
alpha_ij<-subset(param_all_C, Environment==as.character(ca$Env[x]) & Tu_Regime==as.character(ca$FocalSR3[x]) & Te_Regime==as.character(ca$CompSR3[x]) & Replicate==ca$Rep[x])$Tu_inter[1]
lambda<-subset(param_all_C, Environment==as.character(ca$Env[x]) & Tu_Regime==as.character(ca$FocalSR3[x]) & Replicate==ca$Rep[x])$Tu_lambda[1]
}else if(ca$Focalfemale[x]=="Te"){
alpha_i<-subset(param_all_C, Environment==as.character(ca$Env[x]) & Te_Regime==as.character(ca$FocalSR3[x]) & Replicate==ca$Rep[x])$Te_intra[1]
alpha_ij<-subset(param_all_C, Environment==as.character(ca$Env[x]) & Te_Regime==as.character(ca$FocalSR3[x]) & Tu_Regime==as.character(ca$CompSR3[x]) & Replicate==ca$Rep[x])$Te_inter[1]
lambda<-subset(param_all_C, Environment==as.character(ca$Env[x]) & Te_Regime==as.character(ca$FocalSR3[x]) & Replicate==ca$Rep[x])$Te_lambda[1]
}
if(ca$Type[x]=="INTRA"){
densF<-ca$Dens[x]
pred<-lambda*exp(-alpha_i*(densF))
}else if(ca$Type[x]=="INTER"){
densC<-ca$Dens[x]-1
densF<-1
pred<-lambda*exp(-alpha_i*(densF)-alpha_ij*densC)
}
pred
})
ca$pred_D<-sapply(c(1:length(ca$Block)), function(x){
if(ca$Focalfemale[x]=="Tu"){
alpha_i<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Tu_Regime==as.character(ca$FocalSR3[x]) & Replicate==ca$Rep[x])$Tu_intra[1]
alpha_ij<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR3[x] & Te_Regime==ca$CompSR3[x] & Replicate==ca$Rep[x])$Tu_inter[1]
lambda<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR3[x] & Replicate==ca$Rep[x])$Tu_lambda[1]
}else if(ca$Focalfemale[x]=="Te"){
alpha_i<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR3[x] & Replicate==ca$Rep[x])$Te_intra[1]
alpha_ij<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR3[x] & Tu_Regime==ca$CompSR3[x] & Replicate==ca$Rep[x])$Te_inter[1]
lambda<-subset(alphas_mat_D, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR3[x] & Replicate==ca$Rep[x])$Te_lambda[1]
}
if(ca$Type[x]=="INTRA"){
densF<-ca$Dens[x]
pred<-lambda*exp(-alpha_i*(densF))
}else if(ca$Type[x]=="INTER"){
densC<-ca$Dens[x]-1
densF<-1
pred<-lambda*exp(-alpha_i*(densF)-alpha_ij*densC)
}
pred
})
ca$pred_E<-sapply(c(1:length(ca$Block)), function(x){
if(ca$Focalfemale[x]=="Tu"){
alpha_i<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Tu_Regime==as.character(ca$FocalSR3[x]) & Replicate==ca$Rep[x])$Tu_intra[1]
alpha_ij<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR3[x] & Te_Regime==ca$CompSR3[x] & Replicate==ca$Rep[x])$Tu_inter[1]
lambda<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Tu_Regime==ca$FocalSR3[x] & Replicate==ca$Rep[x])$Tu_lambda[1]
}else if(ca$Focalfemale[x]=="Te"){
alpha_i<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR3[x] & Replicate==ca$Rep[x])$Te_intra[1]
alpha_ij<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR3[x] & Tu_Regime==ca$CompSR3[x] & Replicate==ca$Rep[x])$Te_inter[1]
lambda<-subset(alphas_mat_E, Env2==as.character(ca$Env[x]) & Te_Regime==ca$FocalSR3[x] & Replicate==ca$Rep[x])$Te_lambda[1]
}
if(ca$Type[x]=="INTRA"){
densF<-ca$Dens[x]
pred<-lambda*exp(-alpha_i*densF)
}else if(ca$Type[x]=="INTER"){
densC<-ca$Dens[x]-1
densF<-1
pred<-lambda*exp(-alpha_i*(densF)-alpha_ij*densC)
}
pred
})
Do not forget that this is the log of GR +1
euclidean <- function(a, b) sqrt(sum((a - b)^2))
ca$distA<-sapply(c(1:length(ca$Block)), function(x){
euc<-euclidean(ca$pred_A[x], ca$GrowthRateOA[x])
euc
})
ca$distB<-sapply(c(1:length(ca$Block)), function(x){
euc<-euclidean(ca$pred_B[x], ca$GrowthRateOA[x])
euc
})
ca$distC<-sapply(c(1:length(ca$Block)), function(x){
euc<-euclidean(ca$pred_C[x], ca$GrowthRateOA[x])
euc
})
ca$distD<-sapply(c(1:length(ca$Block)), function(x){
euc<-euclidean(ca$pred_D[x], ca$GrowthRateOA[x])
euc
})
ca$distE<-sapply(c(1:length(ca$Block)), function(x){
euc<-euclidean(ca$pred_E[x], ca$GrowthRateOA[x])
euc
})
hist(ca$distA)
hist(ca$distB)
hist(ca$distC)
hist(ca$distD)
hist(ca$distE)
sum(ca$distA, na.rm = TRUE)
[1] 6491.704
sum(ca$distB, na.rm = TRUE)
[1] 6700.173
sum(ca$distC, na.rm = TRUE)
[1] 6571.096
sum(ca$distD, na.rm = TRUE)
[1] 7849.336
sum(ca$distE, na.rm = TRUE)
[1] 6442.68
The smaller sum of euclidean distance is with cxr package, but the lambda estimation does not seem to reflect the real estimates we obtain, so we will use the cxr nested fixed, which is the second best